C# How do I sickle between 4 Colors?

Hi,

1st. I am a beginner, please keep it in C#, no need for more confusion of Java :smile:

How can I cycle 4 colors?

Red, Green, White, Black. (thats the order)

I have assigned a bool and Material prefab for each of the colors, and currently use (GetKey.W) to get the colors to “sickle” or at least what I have for now.

The bools are isRed,isGreen,… etc. and the assigned prefabs are colorRed, colorGreen,… etc.

Here are the bools I use that change the color. have all 4 of them at the bottom of my void Update().

        if (isRed == true)
        {
            gameObject.GetComponent<Renderer> ().sharedMaterial = colorRed;
        }

This is what I have at the top of my void Update.

if(Input.GetKeyDown(KeyCode.W))
        {
            print ("W was pressed");
            if (isRed == true)
            {
                isRed = false;
                isGreen = true;
            }
            isRed = true;
        }

I am aware this wont cycle, since I cant figure out to get to the 3rd color correctly.

Happy for any help to make this cycle from red to green to white to black and back to red.

Thanks a lot!

huh?

edit:

cycle? switch between them in order? (sickle is a cutting tool :slight_smile: )

put the colors in an array, have a “pointer” which you can update to pick the one you want

Sorry, English isnt my first language :smile:

Still dont really understand what I would do though to switch between them in order.

I think what LeftyRighty was saying was, you have an array, something like

string[] colors = new string[4];
string curColor;
...
...
colors[0] = "colorRed";
colors[1] = "colorGreen";
colors[2] = "colorBlue";
colors[3] = "colorYellow";
...
...
curColor = colors[1];

or possibly

string[] colors = new string[4];
int curColor;
...
...
colors[0] = "colorRed";
colors[1] = "colorGreen";
colors[2] = "colorBlue";
colors[3] = "colorYellow";
...
...
//When W is pressed
if(curColor == 3)
{
curColor = 0;
}
else
{
curColor +=1;
}
gameObject.GetComponent<Renderer>().sharedMaterial= colors[curColor];

I could be wrong. That’ just my interpretation of what LeftyRight said.

why would you store colors as strings?

Color[] colors = new Color{Color.Red, Color.Blue, Color.Green, Color.Yellow};

This is how it looks now with leftyRighty, the other way from vedrit brought more errors :confused:

Thanks a lot so far! Appreciate all the help, it looks like it is getting closer.

Because I didn’t know how colors worked. Never messed with them

If you really just want to cycle between the 4 actual MATERIALS, then put them in an array

gameObject.GetComponent<Renderer>().sharedMaterial= coloredMaterials[curMaterial];