Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Cannot change light parameter in start()

Discussion in 'Scripting' started by VincentAbert, Mar 30, 2021.

  1. VincentAbert

    VincentAbert

    Joined:
    May 2, 2020
    Posts:
    120
    Hello !

    So this is is the weirdest thing : Using Unity 2020.3 LTS (HDRP), I can't set a light intensity in the start function this way :

    Code (CSharp):
    1. void Start()
    2.     {
    3.         GetComponent<Light>().intensity = 0;
    4.     }
    5.  
    6.  
    It simply gets overwritten back to its original value immediately.

    The workaround I found is using start as a coroutine and doing this :

    Code (CSharp):
    1.     IEnumerator Start()
    2.     {
    3.         yield return new WaitForEndOfFrame();
    4.         GetComponent<Light>().intensity = 0;
    5.     }
    But this seems super weird, is it normal behaviour, or is it a bug ?

    Thanks !
     
  2. ChexG20

    ChexG20

    Joined:
    Oct 18, 2020
    Posts:
    3
    I'm having the same problem (2021.3.0ft HDRP). I wanted to set light intensity in Awake/Start, but it reverts back to the original scene values after a couple of frames...
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    One thing to try is to Destroy() the light right after you set its intensity.

    This will soon reveal what piece of code might be setting it back because then that code will throw an "object has been destroyed!" error.

    Another thing to look at is animations... if you are animating ANYTHING in the hierarchy where your light is, make sure the
    Write Defaults
    field is unticked in the Animator State(s).
     
  4. ChexG20

    ChexG20

    Joined:
    Oct 18, 2020
    Posts:
    3
    Oh thank you, I really appreciate the ideas and insight!!!