Hello, so i wrote this
var colors : Color[] = new Color[4];
function Start(){
colors[0] = Color.blue;
colors[1] = Color.red;
colors[2] = Color.green;
colors[3] = Color.yellow;
}
function RandomColorBegining()
{
GetComponent(SpriteRenderer).color = colors[Random.Range(0,2,3)];
}
The only thing i want to do is to make random color exluding [1], exluding colors[1] which is red
But i dont know how to i tried researching on google but couldnt find anything could anyone help me :?
Thanks!
Primitive sollution would be to just explicitly bar index 1 from use which could be done like this.
int lIndex = 0;
do
{
lIndex = Random.Range(0, colors.length);
} while(lIndex == 1)
That will basically keep randomizing the color, if you get a 1 it will try again and again until it gets a value that is not 1, you then just index into your array with the resulting value.
I say primitive because this is just 1 value, A better solution would be to have another array of “forbidden from randomization” indicies, or values, and then instead of checking against a single value, you check and re-randomize if the value is contained in that array of banned values. That would give you more control and flexibility moving forward.