Hi there. I’m having an issue with setting the color of a 2D sprite using a C# script.
When the GameObject is instantiated into the level, this script is meant to choose a random number between 0 - 3 and use this integer to pick a value from two arrays, one to set a tag, one to set a color.
private string[] colourTags = {"Cyan", "Yellow", "Magenta", "Purple"};
private Color[] RGBCodes = {new Color(53, 226, 242, 255), new Color(226, 242, 14, 255), new Color(255, 0, 128, 255), new Color(140, 19, 251, 255)};
void Start() {
int randomNumber = Random.Range(0, 3);
gameObject.GetComponent<SpriteRenderer>().color = RGBCodes[randomNumber];
gameObject.tag = colourTags[randomNumber];
}
When the GameObject is instantiated they successfully choose a tag, but when it comes to setting the colour, this doesn’t work as expected. For some reason, only the magenta colour works correctly. All other colours are are white, but when inspecting their colour in the editor, I can see that a similar colour to what was intended has been loaded.
For instance, this is meant to be the cyan colour, yet is visibly white and even has incorrect RGBA values:
Given that I’m relatively new to this, I imagine I’m just doing some rookie mistake. Any help would be appreciated. Thank you.