GetComponent<Light>()

I’m having an weird issue where I can get the light component and the variable is not null but I can’t appear to actually make any changes to the light in code and see them happen.

Code:

private void Start()
{
     Light light = GetComponent<Light>();
     light.intensity = 0.5f;
}

The light’s intensity remains at whatever intensity it was set rather than adjusting in real time. Is this a bug or has something in the workflow changed that I am not aware of?

This code will only be executed as you hit play and then it will just be executed once at the very beginning. If you place a Debug.Log (“Some Text”); in the Start method, you will see exactly when the code is being executed, as the message will appear in the console.

Don’t forget to use code tags whenever you post code:

Thanks, but I’m aware of how the start method works; my issue is that it’s doing nothing regardless of pressing play.

I have already printed debug text to ensure the code is being run (which it is). The console throws no errors; it simply does not make changes to the light component as expected.

Did you make sure you have it placed in the correct game object?

You may easily test it with: Debug.Log (“Some Text”, this);
If you see the message in the console, you can select it, which will then highlight the game object in the hierarchy which is responsible for the message.

Yes, I can actually enable/disable the light by script just fine but the intensity change won’t take place.

I’ve changed a directional light’s intensity to 3 in editor and pasted your code to a script in Unity 5 b12 add the script to the light. Light intensity is being decreased to 0.5 at runtime. So I say it works.
Maybe you should add your version and light type info.

Okay, worth noting that I’m on beta 14. Also since going on beta 14 it would appear that deferred rendering is showing screen space artifacts where point lights are being illuminated:

https://dl.dropboxusercontent.com/u/4946575/Unity 2014-11-27 13-38-17-33.mp4

Having watched your video; I say you accidentally turned the psychedelic rendering mode on :slight_smile:
Anyway, I would say this is asking for a bug report but it’s up to you.

Submitted: Case 651597