Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Unity sprite color messed up.

Discussion in '2D' started by Zenggame, Jul 19, 2019.

  1. Zenggame

    Zenggame

    Joined:
    Jul 19, 2019
    Posts:
    4
    You know that you can change the color of a sprite via a script, right?

    So I tried to do that, like this:

    Code (CSharp):
    1. switch (Game.instance.stage)
    2.         {
    3.             case 1:
    4.                 sprite.color = new Color(69f, 81f, 255f);
    5.                 break;
    6.             case 2:
    7.                 sprite.color = new Color(18f , 207f, 0f);
    8.                 break;
    9.             case 3:
    10.                 sprite.color = new Color(196f, 217f, 42f);
    11.                 break;
    12.             case 4:
    13.                 sprite.color = new Color(128f, 129f, 0f);
    14.                 break;
    15.             case 5:
    16.                 sprite.color = new Color(255f, 110f, 93f);
    17.                 break;
    18.             case 6:
    19.                 sprite.color = new Color(221f, 10f, 0f);
    20.                 break;
    21.         }
    And there doesn't seem to be anything wrong with this, right?

    Surprise surprise? A lot is wrong with it.

    I noticed that my sprite turns white instead of blue. So I selected one of the sprite and it seems that the color of my sprite started glitching out, even in the inspector, like this (if you don't see what's going on,
    basically the blue color that I have chose turns out to be actually white (top right))
    Untitled.png

    This only happens in playmode and when my ChangeColor() method is called in Update().

    What the heck is going on here?
     
  2. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    2,300
    Not really but it's not that obvious. You need to:
    1. choose your colour in the colour picker;
    2. select "RGB 0-1.0",
    3. use the values for the red, blue and green colours that display there in your script.
     
  3. Zenggame

    Zenggame

    Joined:
    Jul 19, 2019
    Posts:
    4
    I
    I'm afraid that's not the "symptom" I was talking about. It's not because the white is 1,1,1 and i set it to 210 and stuff. No no no no. Im talking about this:
    Untitled.png Untitled.png Untitled.png
     
  4. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    2,300
    The way I describe is the way to use when you want to change the colour of anything by script. :)
     
  5. Zenggame

    Zenggame

    Joined:
    Jul 19, 2019
    Posts:
    4
    Do you have any idea of what is happening in the images? cause i dont.
     
  6. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    2,300
    No except that it happens because the setting in the script is not right; and the result says blindingly so. :p
     
  7. Zenggame

    Zenggame

    Joined:
    Jul 19, 2019
    Posts:
    4
    what do you mean by "the setting in the script"?
     
  8. jeffreyschoch

    jeffreyschoch

    Joined:
    Jan 21, 2015
    Posts:
    2,537
    That actually is the problem. You're using values outside the range of the inputs.

    If you change your code to use Color32 instead of Color, you can use the 0-255 range without issue.

    https://docs.unity3d.com/ScriptReference/Color32.html

    You can view a color in the different representations using this dropdown:
    Screen Shot 2019-07-19 at 3.39.09 PM.png
     
    TigerDawn likes this.
  9. TigerDawn

    TigerDawn

    Joined:
    Aug 4, 2018
    Posts:
    18
    Also check to make sure your alpha channel is not set to 100% see through.