Image not changing color via code

Hi,
I’ve been trying to get the color of an image to change if the player has a saved highscore using script. code:

        if(highscore != 0)
        {
            fireImage.color = new  (255, 222, 47);
        }
        else
        {
            fireImage.color = new Color(255, 62, 47);
        }

But when ever my code runs the color just changes to the rgb value “191, 47, 36”
and even though the color wheel shows this code the image actually turns white! I have no clue what’s happening please help!

note: the code posted above is in the Start() method. The first image shows the state before running the game, and the second shows the state of the game when run.

Maybe try using float values in range of 0.0 - 1.0. ? Theres a few different ways to manage colors, but in general, color takes in a float.

new Color(0.25f, 0.64f, 0.12f);

3 Likes

As @Homicide points out, when in doubt, check the docs.

You’ll see there is a Color32 and a Color and they are related and yet take wildly different argument ranges.

2 Likes

This is happening

new Color(25500%, 6200%, 4700%);

gee I wonder why it’s so white.
on a positive note, thank god your HDR is off, otherwise the bloom would eclipse a nearby quasar!

2 Likes

Thanks i figured it out! new code:

        if(highscore != 0)
        {
            fireImage.color = new  (1f, 0.1f, 0f);
        }
        else
        {
            fireImage.color = new Color(1f, 0.9f, 0f);
        }

If this is for a monobehavior, Instead of just typing raw numbers, you could make two public members like:

public Color HighScoreColor;
public Color RegularColor;

Instead of having to figure out which numbers to use, you’ll have a nice color-picker interface in the inspector.

Also, you could change these colors later without having to modify your code.