- Home /
The question is answered, right answer was accepted
Why can't I change the color of a sprite using in C#?
I'm working on a character customization script for my game, yet I've run into the issue of me not being able to change the color of a sprite using Example.GetComponent().color = color defined up above; . In Unity I already went and changed all of the Color values to be the color I would like and they are all opaque, yet whenever I try to change the color nothing happens. I'm using a button system where it changes the number on a variable when pressed, and this script reads what number and decides what to change the sprite to, and everything else works, for example when I try to enable and disable GameObjects it works perfectly fine. Here is my script;
  //CHARACTER PANEL
     public GameObject CharPanel;
 
     //HAIR
     public GameObject Hair1;
     public GameObject Hair2;
     public GameObject Hair3;
     public GameObject HairParent;
 
     //HCOLOR
     public Color HDefaultBrown;
     public Color HBlack;
     public Color HWhite;
     public Color HBlonde;
 
     //CLOTHES
     public Color CDefaultGrey;
     public Color CRed;
     public Color COrange;
     public Color CYellow;
     public Color CGreen;
     public Color CBlue;
     public Color CDarkGrey;
 
     //EYECOLOR
     public Color EDefaultBlue;
     public Color EGreen;
     public Color EAquamarine;
     public Color EBrown;
 
     //VARIABLES
     public float HairS;
     public float HairC;
     public float CC;
     public float EyeC;
 
     //EFFECTED GAMEOBJECTS
     public GameObject Clothing;
     public GameObject Eye1;
     public GameObject Eye2;
 
     void Update()
     {
         //VARIABLE DEFINITION
         HairS = CharPanel.GetComponent<CharCustom>().HairS;
         HairC = CharPanel.GetComponent<CharCustom>().HairC;
         CC = CharPanel.GetComponent<CharCustom>().CC;
         EyeC = CharPanel.GetComponent<CharCustom>().EyeC;
 
         //HAIR CHANGER
         if (HairS == 0)
         {
             Hair1.SetActive(false);
             Hair2.SetActive(false);
             Hair3.SetActive(false);
         }
         if (HairS == 1)
         {
             Hair1.SetActive(true);
             Hair2.SetActive(false);
             Hair3.SetActive(false);
         }
         if (HairS == 2)
         {
             Hair1.SetActive(false);
             Hair2.SetActive(true);
             Hair3.SetActive(false);
         }
         if (HairS == 3)
         {
             Hair1.SetActive(false);
             Hair2.SetActive(false);
             Hair3.SetActive(true);
         }
 
         //HAIR COLOR CHANGER
         if (HairC == 0)
         {
             HairParent.GetComponentInChildren<SpriteRenderer>().color = HDefaultBrown;
         }
         if (HairC == 1)
         {
             HairParent.GetComponentInChildren<SpriteRenderer>().color = HBlack;
         }
         if (HairC == 2)
         {
             HairParent.GetComponentInChildren<SpriteRenderer>().color = HWhite;
         }
         if (HairC == 3)
         {
             HairParent.GetComponentInChildren<SpriteRenderer>().color = HBlonde;
         }
 
         //CLOTHES COLOR CHANGER
         if (CC == 0)
         {
             Clothing.GetComponent<SpriteRenderer>().color = CDefaultGrey;
         }
         if (CC == 1)
         {
             Clothing.GetComponent<SpriteRenderer>().color = CRed;
         }
         if (CC == 2)
         {
             Clothing.GetComponent<SpriteRenderer>().color = COrange;
         }
         if (CC == 3)
         {
             Clothing.GetComponent<SpriteRenderer>().color = CYellow;
         }
         if (CC == 4)
         {
             Clothing.GetComponent<SpriteRenderer>().color = CGreen;
         }
         if (CC == 5)
         {
             Clothing.GetComponent<SpriteRenderer>().color = CBlue;
         }
         if (CC == 6)
         {
             Clothing.GetComponent<SpriteRenderer>().color = CDarkGrey;
         }
 
         //EYE COLOR CHANGER
         if (EyeC == 0)
         {
             Clothing.GetComponent<SpriteRenderer>().color = EDefaultBlue;
         }
         if (EyeC == 1)
         {
             Clothing.GetComponent<SpriteRenderer>().color = EGreen;
         }
         if (EyeC == 2)
         {
             Clothing.GetComponent<SpriteRenderer>().color = EAquamarine;
         }
         if (EyeC == 3)
         {
             Clothing.GetComponent<SpriteRenderer>().color = EBrown;
         }
     }
Any ideas?
Answer by Electronicall · Mar 10, 2019 at 03:56 PM
Thanks for the suggestion, but that didn't seem to fix the issue. However, I believe I have discovered the cause after looking into it more. In order to have the sprite colors change, I had to have the respective parts of the color changing script on the GameObjects themselves. For example, I made a new script specifically for the clothing and put it on the clothing GameObject, and when I tested it, it worked properly. There were also a few small errors that i didn't notice before. The issue is now resolved.
Answer by dan_wipf · Mar 10, 2019 at 07:01 AM
Well can it be because you havent asign a Color to all your color porperties? for example HDefaultBrown = new Color(165,42,42); //165,42,42 is the RGB Value for Brown 
?
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Instantiating multiple sprites and assigning different colors for each 1 Answer
Question on Sprite and Movement 0 Answers
Strange behavour when drawing on texture 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                