Random colors?
Ok I have a script which is run to give 4 different balls random colors that I have chosen. The problem is that I don't want any of the balls to be the same color but they are. How do I fix this? Here is 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 colorBlue; public Color colorYellow;
// Use this for initialization
void Start () {
SetColor ();
}
// Update is called once per frame
void Update () {
}
void SetColor () {
int index = Random.Range (0, 3);
switch (index) {
case 0:
currentColor = "Green";
sr.color = colorGreen;
break;
case 1:
currentColor = "Red";
sr.color = colorRed;
break;
case 2:
currentColor = "Blue";
sr.color = colorBlue;
break;
case 3:
currentColor = "Yellow";
sr.color = colorYellow;
break;
}
int i = Random.Range (0, 3);
switch (i) {
case 0:
currentColor = "Green";
sr1.color = colorGreen;
break;
case 1:
currentColor = "Red";
sr1.color = colorRed;
break;
case 2:
currentColor = "Blue";
sr1.color = colorBlue;
break;
case 3:
currentColor = "Yellow";
sr1.color = colorYellow;
break;
}
int ind = Random.Range (0, 3);
switch (ind) {
case 0:
currentColor = "Green";
sr2.color = colorGreen;
break;
case 1:
currentColor = "Red";
sr2.color = colorRed;
break;
case 2:
currentColor = "Blue";
sr2.color = colorBlue;
break;
case 3:
currentColor = "Yellow";
sr2.color = colorYellow;
break;
}
int wow = Random.Range (0, 3);
switch (wow) {
case 0:
currentColor = "Green";
sr3.color = colorGreen;
break;
case 1:
currentColor = "Red";
sr3.color = colorRed;
break;
case 2:
currentColor = "Blue";
sr3.color = colorBlue;
break;
case 3:
currentColor = "Yellow";
sr3.color = colorYellow;
break;
}
int cool = Random.Range (0, 3);
switch (cool) {
case 0:
currentColor = "Green";
sr4.color = colorGreen;
break;
case 1:
currentColor = "Red";
sr4.color = colorRed;
break;
case 2:
currentColor = "Blue";
sr4.color = colorBlue;
break;
case 3:
currentColor = "Yellow";
sr4.color = colorYellow;
break;
}
}
Comment