Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

GetComponent<Light>()

Discussion in 'Unity 5 Pre-order Beta' started by Brenden-Frank, Nov 27, 2014.

  1. Brenden-Frank

    Brenden-Frank

    Joined:
    Aug 5, 2012
    Posts:
    110
    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:

    Code (csharp):
    1.  
    2. private void Start()
    3. {
    4.      Light light = GetComponent<Light>();
    5.      light.intensity = 0.5f;
    6. }
    7.  
    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?
     
    Last edited: Nov 27, 2014
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    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:
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
  3. Brenden-Frank

    Brenden-Frank

    Joined:
    Aug 5, 2012
    Posts:
    110
    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.
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    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.
     
  5. Brenden-Frank

    Brenden-Frank

    Joined:
    Aug 5, 2012
    Posts:
    110
    Yes, I can actually enable/disable the light by script just fine but the intensity change won't take place.
     
  6. gurayg

    gurayg

    Joined:
    Nov 28, 2013
    Posts:
    269
    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.
     
  7. Brenden-Frank

    Brenden-Frank

    Joined:
    Aug 5, 2012
    Posts:
    110
  8. gurayg

    gurayg

    Joined:
    Nov 28, 2013
    Posts:
    269
    Having watched your video; I say you accidentally turned the psychedelic rendering mode on :)
    Anyway, I would say this is asking for a bug report but it's up to you.
     
  9. Brenden-Frank

    Brenden-Frank

    Joined:
    Aug 5, 2012
    Posts:
    110
    Submitted: Case 651597