Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Sprite disappear when changing color

Discussion in '2D' started by xeonheart, Jul 24, 2021.

  1. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    Hello,

    I am trying to create a character custom, which is just changing the color if button is pressed... here is the code below:

    Code (CSharp):
    1.     public Color Black;
    2.     public Color Blue;
    3.     public Color Red;
    4.     public Color Green;
    5.  
    6. private int P_Shirt_Index = 0;
    7.  
    8.         if (P_Shirt_Index == 0)
    9.         {
    10.             m_shirt.color = Red;
    11.             f_shirt.color = Red;
    12.         }
    13.         if (P_Shirt_Index == 1)
    14.         {
    15.             m_shirt.color = Blue;
    16.             f_shirt.color = Blue;
    17.         }
    18.         if (P_Shirt_Index == 2)
    19.         {
    20.             m_shirt.color = Purple;
    21.             f_shirt.color = Purple;
    22.         }
    23.         if (P_Shirt_Index == 3)
    24.         {
    25.             m_shirt.color = Green;
    26.             f_shirt.color = Green;
    27.         }
    however any button i press, makes the sprite disappear altogether... any ideas?
     
  2. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    ok i see now whats happening, however not sure how to stop it, so the alpha is changing from 255 to 0, when i click the button to change color for the sprite... but not sure if I need to program a setting in C# code every time I change the color of the sprite... or if I can set it on the UI of unity?
     
    Last edited: Jul 24, 2021
  3. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    You are correct. You can set the Alpha of each color to 255 in the inspector. Click on each color and set the "A" slider to 255. The default is 0 on created variables.
     
    xeonheart likes this.