1

I am very new to both C# and Unity, so I have been following some tutorials to create a Wall Torch (GameObject) that has a child 2D Spot Light which flickers using the following script I found on Github. https://gist.github.com/sinbad/4a9ded6b00cf6063c36a4837b15df969 According to Visual Studio, the script has no errors.

However, upon hitting Play to start the script, nothing happens. There are no errors or messages in the Console.

Example

I have attached the script as a component to the 2D light, and made sure the class and script name are identical.

I have tried restarting the project and creating a new one, as well as deleting and recreating the script. The Unity project is 2D with URP.

I really appreciate any help.

3
  • 1
    Add debug logs in the script and see if those log messages are getting printed. If not, then script is probably not getting executed. Commented Mar 12, 2024 at 8:48
  • Please add debug log on the Start method and on the update method after the null check to see if something happen, if something happen so try to add break points and debug what happen Commented Mar 12, 2024 at 10:34
  • Light2D != Light => GetComponent<Light> will return null ... the script stupidly hides this with if (light == null) return; and makes the whole component silently fail .. one of the classic cases where you WANT to get an exception rather than hiding it ^^ Commented Mar 12, 2024 at 12:32

1 Answer 1

0

I haven´t tested this, but I think the problem is that the script tries to get a Light component from your GameObject, but you have a Light2D component. Try replacing the public new Light light; line in the script with public Light2D light;, and on line 45 change Light to Light2D, and it should work;

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that was a large part of the problem. Also, as the original code was written for 3d, the new code must include using UnityEngine.Rendering.Universal; and as such float newVal = Random.Range(minIntensity, maxIntensity); must be changed to float newVal = UnityEngine.Random.Range(minIntensity, maxIntensity); This then works perfectly. I also posted a fork on the original script containing the updated code; gist.github.com/lucasmhdev/b9593a07a9a3e72ea681fa3eb9ef0898.js Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.