- Home /
Make sure that game objects aren't the same color
Ok in my game I have 4 balls. I have a script that makes the balls change to a random color. How could I make sure that none of the balls are the same color? Here is my script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Everything : MonoBehaviour { public string currentColor; public SpriteRenderer sr; public SpriteRenderer sr1; public SpriteRenderer sr2; public SpriteRenderer sr3; public SpriteRenderer sr4; public Color colorGreen; public Color colorRed; public Color colorBlack; public Color colorBlue; public Color colorOrange; public Color colorYellow; public Color colorBrown; // Use this for initialization
void Start () {
SetRandomColor ();
}
// Update is called once per frame
void Update () {
}
void SetRandomColor () {
sr1.color != sr2.color;
sr1.color != sr3.color;
sr1.color != sr4.color;
sr2.color != sr3.color;
sr2.color != sr4.color;
sr3.color != sr4.color;
int index = Random.Range (0, 6);
switch (index) {
case 0:
currentColor = "Green";
sr.color = colorGreen;
break;
case 1:
currentColor = "Red";
sr.color = colorRed;
break;
case 2:
currentColor = "Black";
sr.color = colorBlack;
break;
case 3:
currentColor = "Blue";
sr.color = colorBlue;
break;
case 4:
currentColor = "Orange";
sr.color = colorOrange;
break;
case 5:
currentColor = "Yellow";
sr.color = colorYellow;
break;
case 6:
currentColor = "Brown";
sr.color = colorBrown;
break;
}
int help = Random.Range (0, 6);
switch (help) {
case 0:
currentColor = "Green";
sr1.color = colorGreen;
break;
case 1:
currentColor = "Red";
sr1.color = colorRed;
break;
case 2:
currentColor = "Black";
sr1.color = colorBlack;
break;
case 3:
currentColor = "Blue";
sr1.color = colorBlue;
break;
case 4:
currentColor = "Orange";
sr1.color = colorOrange;
break;
case 5:
currentColor = "Yellow";
sr1.color = colorYellow;
break;
case 6:
currentColor = "Brown";
sr1.color = colorBrown;
break;
}
int yes = Random.Range (0, 6);
switch (yes) {
case 0:
currentColor = "Green";
sr2.color = colorGreen;
break;
case 1:
currentColor = "Red";
sr2.color = colorRed;
break;
case 2:
currentColor = "Black";
sr2.color = colorBlack;
break;
case 3:
currentColor = "Blue";
sr2.color = colorBlue;
break;
case 4:
currentColor = "Orange";
sr2.color = colorOrange;
break;
case 5:
currentColor = "Yellow";
sr2.color = colorYellow;
break;
case 6:
currentColor = "Brown";
sr2.color = colorBrown;
break;
}
int omg = Random.Range (0, 6);
switch (omg) {
case 0:
currentColor = "Green";
sr3.color = colorGreen;
break;
case 1:
currentColor = "Red";
sr3.color = colorRed;
break;
case 2:
currentColor = "Black";
sr3.color = colorBlack;
break;
case 3:
currentColor = "Blue";
sr3.color = colorBlue;
break;
case 4:
currentColor = "Orange";
sr3.color = colorOrange;
break;
case 5:
currentColor = "Yellow";
sr3.color = colorYellow;
break;
case 6:
currentColor = "Brown";
sr3.color = colorBrown;
break;
}
int yay = Random.Range (0, 6);
switch (yay) {
case 0:
currentColor = "Green";
sr4.color = colorGreen;
break;
case 1:
currentColor = "Red";
sr4.color = colorRed;
break;
case 2:
currentColor = "Black";
sr4.color = colorBlack;
break;
case 3:
currentColor = "Blue";
sr4.color = colorBlue;
break;
case 4:
currentColor = "Orange";
sr4.color = colorOrange;
break;
case 5:
currentColor = "Yellow";
sr4.color = colorYellow;
break;
case 6:
currentColor = "Brown";
sr4.color = colorBrown;
break;
}
}
}
Answer by toddisarockstar · Aug 10, 2017 at 03:28 AM
try this. balls will never be the same color
public SpriteRenderer sr;
public SpriteRenderer sr1;
public SpriteRenderer sr2;
public SpriteRenderer sr3;
public SpriteRenderer sr4;
public SpriteRenderer[] balls;
int i;int i2;
public Color[] colors = new Color[]{Color.red,Color.green,Color.yellow,Color.black,Color.cyan,Color.magenta,Color.white};
public string used;
void Start () {
balls = new SpriteRenderer[]{sr,sr1,sr2,sr3,sr4};
used = "";
i = balls.Length;
while(i>0){i--;
bool b = false;
while(!b){
i2=Random.Range(0,colors.Length);
if(!used.Contains(" "+i2))
{b=true;used=used+" "+i2;}}
balls[i].color=colors[i2];}}
i edited my answer to fit what you are doing a bit better!
Answer by KenjiKyo · Aug 17, 2017 at 11:38 PM
How about having a List<Color>
that you initialize with all desired colors that can be chosen from. Some kind of color selection pool. Call Random.Range()
between 0 and the list size to get a random color index, and remove that color from the list to apply it to any given ball. If a ball has to change to a new color, re-insert its color into the list to make it available for future random color picking.
Your answer
![](https://koobas.hobune.stream/wayback/20220612131339im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I randomly create 2 objects, each with a different shape and color? 1 Answer
how to change color in unity scripting overtime?,how to change color gradually with C# 2 Answers
Randomize a spawn position without repition 3 Answers
Randomize texture within prefab 0 Answers
Color.lerp not working properly 2 Answers