Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

changing color on a sprite renderer

Discussion in '2D' started by xeonheart, Apr 27, 2021.

  1. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    114
    Hello,

    I am trying to change the color on a sprite/game object so the player has a option to change colors, however when I do set it, the object or part of it disappears, instead of changing color, example below:

    Code (CSharp):
    1.  
    2. public GameObject Female_char;
    3.  
    4. public SpriteRenderer f_hair;
    5.  
    6. public int P_Gender_Index = 0;
    7.  
    8. public int P_Hair_Index = 0;
    9.  
    10.  
    11. if (P_Gender_Index == 0)
    12.         {
    13.             if (P_Hair_Index == 0)
    14.             {
    15.                 f_hair.color = Black;
    16.             }
    17.             if (P_Hair_Index == 1)
    18.             {
    19.                 f_hair.color = Blue;
    20.             }
    21.             if (P_Hair_Index == 2)
    22.             {
    23.                 f_hair.color = Red;
    24.             }
    25.             if (P_Hair_Index == 3)
    26.             {
    27.                 f_hair.color = Green;
    28.             }
    29.         }
    30.  
    I do have a method when buttons are pressed, it changes the P_Hair_Index whether P_Hair_Index++ or P_Hair_Index--.


    not sure if there is any additional info needed, but let me know.
     
  2. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    114
    k i literally just tried:
    Code (CSharp):
    1. f_hair.color = Black;
    so tried to set it to black, and the object or the part of the hair of the character just disappears... nothing changes on the properties, like nothing changes such as it disabled/false or hides it, it doesnt change the location of it, it doesnt change the sorting order/layer to anything, i also check the order number and remains the same :(

    is this a bug?
     
  3. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    316
    use color.Black or color.Red

    also, changing color like this will probably not give you the results you want, it only works correctly if the sprite you are changing color is grayscale and even then the range of colors you can get is quite limited
     
  4. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    114
    gotya thank you for that, i do see a difference, like blue, its really dark, but what i tried to use for the public Color Tan etc. is use the color wheel (see attached for image), i know its all black, but when i change it to Tan or blue, I thought with that public Color Tan would assign the color like so:

    Code (CSharp):
    1. f_hair.color = Black;
    but i guess that wouldnt work?
     

    Attached Files:

  5. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    316
    hmm it seems you have alpha = 0, you gotta put alpha to 255 on your colors

    also i think color passes as value type, so you have to reset it after changing it in the inspector like that
     
    xeonheart likes this.
  6. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    114
    gotya thank you, so i would set Alpha = 0, how or where would i do that? thanks in advance
     
  7. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    316
    make sure your slider is like this

    index.jpg
     
    xeonheart likes this.
  8. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    114
    AWESOME IT WORKS, thank you sir :)
     
    rarac likes this.
unityunity