SpriteRenderer.Color not working as expected.

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.

Yep, but a completely understandable one. Colors are meant to be float values with the rgba values all being between 0 and 1. You are using them as int values, totally understandable as that is how the editor presents them to you, but not how the scripts actually expect them. And, to top this off, anything greater than 1 is totally valid, and just the equivalent of 1 so no errors are thrown. You’ll notice the only reason one color is working is it has a 0 in it.

Thank you so much, that was exactly what it was! I changed Color to Color32 so that it could accept values between 0 and 255 and now it works as expected. Thanks again.