Script that makes it so colors don't repeat

Hi there dear Unity Community!I am currently a begginer in C# and Unity and I just feel like it is a good idea to ask over here one of my ideas that I can’t seem to implement properly.So I am basically trying to make some sort of a game that is similar to color switch by the thing that you can switch colors and you can only go through specific colors, but a little bit different.I’m just trying to understand how things work at this point haha.So here is my code first things first:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ColorManager : MonoBehaviour
{
    // Start is called before the first frame update

    public string currentColor;
    public SpriteRenderer sr;
    public Color colorcyan;
    public Color coloryellow;
    void Start()
    {
        SetRandomColor();
    }

    void SetRandomColor()
    {
        int index = Random.Range(0, 2);

        switch (index)
        {
            case 0:
                currentColor = "cyan";
                sr.color = colorcyan;
                break;
            case 1:
                currentColor = "yellow";
                sr.color = coloryellow;
                break;
        }
    }


    public void OnTriggerEnter2D(Collider2D col)
    {
        if (col.tag == "colorchanger")
        {
            SetRandomColor();
            Destroy(col.gameObject);
            return;
        }

        if (col.tag != currentColor)
        {
            SceneManager.LoadScene(0);
        }

    }
}

As you can see, this is the script that deals with changing the colors and making the game reset if you don’t match the color.What I’m trying to add is something that could check what the current color is and then it should give the player another color from the list, not the same one ( for now there is only 2 colors, but I’m planning to add more of them).The colorchanger tag in the OnTriggerEnter2D is the trigger object that is supposed to change colors like that but right now I can’t figure it out by myself to be honest, 2 hours already passed. :confused:
So, I was wondering…How could I apply this so it would work as it should?
Anticipated Thanks! :slight_smile:

Easiest thing to do is to make a List of the available colors, remove the current color from the list, and choose randomly from what remains.

List<string> availableColors = new List<string>();
availableColors.Add("cyan");
availableColors.Add("yellow");
availableColors.Remove(currentColor);
currentColor = availableColors[Random.Range(0, availableColors.Count)];

Turning that color string into a color value, you’ll want to probably use a Dictionary<string, Color>:

Dictionary<string, Color> colorLookup = new Dictionary<string, Color>();
colorLookup.Add("cyan", colorcyan);
//and you can then use this for the list, instead of lines 2-3 in the above code
List<string> availableColors = new List<string>(colorLookup.Keys);
....
sr.color = colorLookup[currentColor];
2 Likes

Am I doing something wrong or why is neither of them working properly?
I didn’t really understand what is happening over here completely to be honest, could you further explain?
As I didn’t really get how to apply this for my script properly and I want to learn while trying to do it. :slight_smile:

Paste your current code (after attempting to integrate my examples), and I can help figure out what’s wrong.

1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ColorManager : MonoBehaviour
{
    public string currentColor;
    public SpriteRenderer sr;
    public Color colorcyan;
    public Color coloryellow;
    void Start()
    {
        SetRandomColor();
    }
    void SetRandomColor()
    {
        int index = Random.Range(0, 2);
        switch (index)
        {
            case 0:
                currentColor = "cyan";
                sr.color = colorcyan;
                break;
            case 1:
                currentColor = "yellow";
                sr.color = coloryellow;
                break;
        }
    }
    public void OnTriggerEnter2D(Collider2D col)
    {
        if (col.tag == "colorchanger")
        {
       

Dictionary<string, Color> colorLookup = new Dictionary<string, Color>();
colorLookup.Add("cyan", colorcyan);
colorLookup.Add("yellow", coloryellow);
List<string> availableColors = new List<string>(colorLookup.Keys);
currentColor = availableColors[Random.Range(0, availableColors.Count)];

sr.color = colorLookup[currentColor];
            Destroy(col.gameObject);
            return;
        }
        if (col.tag != currentColor)
        {
            SceneManager.LoadScene(0);
        }
    }
}

Edit: Okay Lmao I don’t know why it inserted some links when I put my code in here but I deleted them

You didn’t remove the current color from the list (line 4 from my first code block should be inserted after line 40 of your code). Other than that, it looks like it should work.

1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ColorManager : MonoBehaviour
{
    // Start is called before the first frame update

    public string currentColor;
    public SpriteRenderer sr;
    public Color colorcyan;
    public Color coloryellow;
    void Start()
    {
        SetRandomColor();
    }

    void SetRandomColor()
    {
        int index = Random.Range(0, 2);

        switch (index)
        {
            case 0:
                currentColor = "cyan";
                sr.color = colorcyan;
                break;
            case 1:
                currentColor = "yellow";
                sr.color = coloryellow;
                break;
        }
    }


    public void OnTriggerEnter2D(Collider2D col)
    {
        if (col.tag == "colorchanger")
        {
            Dictionary<string, Color> colorLookup = new Dictionary<string, Color>();
            colorLookup.Add("cyan", colorcyan);
            colorLookup.Add("yellow", coloryellow);
            List<string> availableColors = new List<string>(colorLookup.Keys);
            currentColor = availableColors[Random.Range(0, availableColors.Count)];
            availableColors.Remove(currentColor);
            sr.color = colorLookup[currentColor];
            Destroy(col.gameObject);
            return;
        }

        if (col.tag != currentColor)
        {
            SceneManager.LoadScene(0);
        }

    }
}

Last edit: I have deleted a line from your code somehow but even after putting it back the rendering is here but it still happens that my color stays the same sometimes.
Also, how could I integrate something so the circle trigger makes the sprite that gives the car the color be another color that is different from the car’s currentColor and it’s gonna give the same color as the circle’s current color.

So, do you have any other suggestions based on what I gave you? :slight_smile:

The “remove” line is in the wrong place. It needs to be before you choose a random item.

1 Like

Instead of storing currentColor as a string store it as a Color. Then when you set a random color, check if its the same as the current color. If it is the same, call the set random color function again. Do this untill it randomly chooses a color that is different from the last color (stored as currentColor which you might want to change the variable name).

You’ll need to check the color before you set it so that the color doesn’t change until you find a color that is different from your current (last).

This is called recursion, where a function calls itself within its own code.

1 Like

Thanks guys! I have managed to make it work now finally haha, feels great ! :smile: