Problem with a color randomization

So I’m making a 2d game for android where the user is given a circle with a certain color and then, in the middle of other circles with random colors, he has to guess which one has the same color as the one that was given to him.

My problem is that sometimes the random colors from the other circles are very similar to the color of the circle that was given to the user and that may create some confusion to the user. I wanna make the color from the other circles vary taking into account the color of the circle that was given to the user. I already tried to use if’s to see if the colors are very similar to randomize them again but that just makes unity crash. If anyone could help me out, that would be great :slight_smile:

Btw I’m using the sprite renderer from the circles to randomize the color.

What’s the code making Unity crash? Unity crashes while playing the game are almost everytime caused by infinite loops, so you probably wrote a loop to create another random color and forgot something to break the loop.

To check if one color is similar to another you could convert them to Vector3s (using the rgb values) or treat them as Vector4s, and then using the Vector3.Distance or Vector4.Distance methods to check against a threeshold.

It should be something like:

while (Vector4.Distance(randomColor,circleColor) < threeshold) {
    randomColor = new Color(Random.Range(0.0,1.0),Random.Range(0.0,1.0),Random.Range(0.0,1.0));
}

From this Stack overflow question

 public bool AreColorsSimilar(Color c1, Color c2, int tolerance)
 {
     return Math.Abs(c1.R - c2.R) < tolerance &&
            Math.Abs(c1.G - c2.G) < tolerance &&
            Math.Abs(c1.B - c2.B) < tolerance;
 }

As the answer states, if this doesn’t work well enough for you you might need to convert to the HSV color space and do a similar comparison there on the hue, saturation, and brightness.