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

How to Have Point Light Change to Random Colors?

Discussion in 'Scripting' started by LinkUpGames, Mar 1, 2019.

  1. LinkUpGames

    LinkUpGames

    Joined:
    Dec 11, 2013
    Posts:
    19
    Hi there, currently I'm trying to add in a little fun thing into one of my levels wherein pressing a button causes some lights pointing at targets to start randomly changing colors like a rave. I tried hodge podgeing it together and what I've ended up with is some bad code that causes the light to spazz out as a billion different colors a second rather than a nice transition.

    Code (CSharp):
    1.     public Light buttonLight;
    2.     public Light leftLight;
    3.     public Light middleLight;
    4.     public Light rightLight;
    5.  
    6.     public bool changeColor = true;
    7.  
    8.     // Update is called once per frame
    9.     void Update()
    10.     {
    11.         if (changeColor)
    12.         {
    13.             ColorChange();
    14.             changeColor = false;
    15.         }
    16.         else if (!changeColor)
    17.         {
    18.             StartCoroutine(Delay());
    19.             changeColor = true;
    20.         }
    21.        
    22.     }
    23.  
    24.     IEnumerator Delay()
    25.     {
    26.         yield return new WaitForSeconds(3);
    27.     }
    28.  
    29.     void ColorChange()
    30.     {
    31.         buttonLight.color = new Color32(System.Convert.ToByte(Random.Range(0, 255)), System.Convert.ToByte(Random.Range(0, 255)), System.Convert.ToByte(Random.Range(0, 255)), 255);
    32.     }
    It's currently set-up to make the light inside the button change colors and not the 3 spotlights I have set up. I wanted to figure it out for the button light first and go from there. Any help would be appreciated! Thanks!
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Coroutines don't work the way you seem to think they do. Although they don't run on a separate thread, they also don't block the Update() method from being called every frame. So, your attempt to call 'StartCoroutine(Delay());' merely calls a method that waits a little while, then exits, without affecting anything else.

    You probably want to write your whole color change code as a coroutine. Something like this:

    Code (CSharp):
    1. void Start() {
    2.     StartCoroutine(ColorChangeRoutine());
    3. }
    4.  
    5. private IEnumerator ColorChangeRoutine() {
    6.     while (true) {
    7.         buttonLight.color = new Color32(System.Convert.ToByte(Random.Range(0, 255)), System.Convert.ToByte(Random.Range(0, 255)), System.Convert.ToByte(Random.Range(0, 255)), 255);
    8.         yield return new WaitForSeconds(3);
    9.     }
    10. }
    Once you've got that working, you can consider using Color.Lerp to more smoothly transition between colors if you want. (Sorry if there are any typos here...)

    Code (CSharp):
    1. void Start() {
    2.     StartCoroutine(ColorChangeRoutine());
    3. }
    4.  
    5. private IEnumerator ColorChangeRoutine() {
    6.     while (true) {
    7.         var startColor = buttonLight.color;
    8.         var endColor = new Color32(System.Convert.ToByte(Random.Range(0, 255)), System.Convert.ToByte(Random.Range(0, 255)), System.Convert.ToByte(Random.Range(0, 255)), 255);
    9.        
    10.         var t = 0;
    11.         while (t < 1) {
    12.             t = Mathf.Min(1, lerp + Time.deltaTime; // Multiply Time.deltaTime by some constant to speed/slow the transition.
    13.             buttonLight.color = Color.Lerp(startColor, endColor, t);
    14.             yield return null;
    15.         }
    16.        
    17.         yield return new WaitForSeconds(3);
    18.     }
    19. }
     
  3. LinkUpGames

    LinkUpGames

    Joined:
    Dec 11, 2013
    Posts:
    19
    Awesome! I'll just get it working first and then transition into the smooth color changing. Thanks so much for the help! :)

    EDIT: Additionally, you use "lerp" inside of the Mathf.Min bit but don't define it elsewhere, contextually what is "lerp" meant to be in this case?
     
    Last edited: Mar 1, 2019
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,022
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Oops, that 'lerp' should be 't'. I changed the variable name but didn't change it everywhere.