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. Dismiss Notice

Colors are confusing

Discussion in 'Scripting' started by Khaoscraft, Sep 19, 2020.

  1. Khaoscraft

    Khaoscraft

    Joined:
    Jul 23, 2020
    Posts:
    10
    I have this little script, which is intended to blink the color of these two lines' material from white to black. When I run it, it only changes to white once, then doesn't change again afterwards. Yes, I have set a value for blinkspeed in the inspector, and I made it that way so that I can have different blink speeds for each level individually(I'm sure you understand)

    Code (CSharp):
    1. public float blinkspeed = 0.0f;
    2.     Color Goodblue = new Color(0, 50, 256);
    3.     private void Awake()
    4.     {
    5.         InvokeRepeating("Blink", blinkspeed, blinkspeed);
    6.     }
    7.     void Blink()
    8.     {
    9.         transform.GetComponent<Renderer>().material.color = Color.white;
    10.         Invoke("Unblink", blinkspeed);
    11.     }
    12.     void Unblink()
    13.     {
    14.         transform.GetComponent<Renderer>().material.color = Color.black;
    15.     }
    16. }
     

    Attached Files:

  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    First of all, if your "blinkspeed" is really 0 (and that hasn't been changed somewhere), then that's a problem. Not sure whether Unity is smart enough to notice that and give up or if it's going to cause an infinite loop or if it's just going to run once per frame, but regardless, you shouldn't be doing that.

    Even if you have changed that to a different value in the inspector or something, having code that initializes it to zero is unsafe, you should specify a default value that will actually work.

    (Also, it's fairly misleading that you named this variable blinkspeed but you are using it as the delay between blinks, meaning that higher values are slower, not faster.)


    Secondly, you are telling it to blink and unblink at the exact same time. For example, if blinkspeed were 5, then you're telling Blink to run at times 5, 10, 15, 20, etc. When Blink runs at time 5, it schedules Unblink to run at time 10. When Blink runs at time 10, it schedules Unblink to run at time 15. So Unblink runs at times 10, 15, 20, etc.

    That means at time 10, it tries to both Blink and Unblink, and whichever one of them happens to run later in the frame will "win", and you'll never see the result from the other one. Then at time 15, it tries again to both Blink and Unblink, etc.

    Try using half of blinkspeed on line 10.


    Also note Color uses rgb values in the range 0-1, so your "Goodblue" isn't what you think it is.
     
    Bunny83 likes this.
  3. Khaoscraft

    Khaoscraft

    Joined:
    Jul 23, 2020
    Posts:
    10
    Thank you, halving the blinkspeed worked
     
  4. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    719
    Also, remember that 'Color32' is 0 to 255 for rgb, while 'Color' is 0.0f to 1.0f for rgb, just a nice thing to know.
     
    Bunny83 likes this.