This is for unity iPhone but it’s more of scripting question so I’ll post it here.
I have for sections in my game : Blue , Red, Green and , Yellow
Each is texture . I also have an array that stores values 1,2,3,4 where for me 1 represents blue, 2=red, 3=green. 4 = yellow.
I have a randomize script that will swap each of these colour values with each section/zone.
The problem is that to do the checks I have quite a few ‘If’ statments as you’ll see bellow.
The code works but is there a better way to do the checks?
function Randomize()
{
var arr = new Array ();
// Resize the array
arr.length = 4;
// Add data
// 1 = blue , 2= red, 3 = green, 4 = yellow
//each zone will be assigned a colour randomly.
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
// iterate through the array to give each zone a random number(colour value).
for (var i = 0; i < 4; ++i)
{
var index = Random.Range(0,arr.length);
zones[i] = arr[index];
/// HANDLE ZONE 1 COLOURS ///
/// i is just the zone-1 (due to the way arrays start at 0 )
//If zone one is currently selected in loop and colour 1 (blue) is choosen apply blue texture.
if (i==0 zones[i]== 1)
{
zone1.renderer.material.mainTexture = blueTexture;
}
if (i==0 zones[i]== 2)
{
zone1.renderer.material.mainTexture = redTexture;
}
if (i==0 zones[i]== 3)
{
zone1.renderer.material.mainTexture = greenTexture;
}
if (i==0 zones[i]== 4)
{
zone1.renderer.material.mainTexture = yellowTexture;
}
///HANDLE ZONE 2 COLOURS///
///i =1
if (i==1 zones[i]== 1)
{
zone2.renderer.material.mainTexture = blueTexture;
}
if (i==1 zones[i]== 2)
{
zone2.renderer.material.mainTexture = redTexture;
}
if (i==1 zones[i]== 3)
{
zone2.renderer.material.mainTexture = greenTexture;
}
if (i==1 zones[i]== 4)
{
zone2.renderer.material.mainTexture = yellowTexture;
}
/// HANDLE ZONE 3 COLOURS
///i==2
if (i==2 zones[i]== 1)
{
zone3.renderer.material.mainTexture = blueTexture;
}
if (i==2 zones[i]== 2)
{
zone3.renderer.material.mainTexture = redTexture;
}
if (i==2 zones[i]== 3)
{
zone3.renderer.material.mainTexture = greenTexture;
}
if (i==2 zones[i]== 4)
{
zone3.renderer.material.mainTexture = yellowTexture;
}
///HANDLE ZONE 4 COLOURS
/// i ==3
if (i==3 zones[i]== 1)
{
zone4.renderer.material.mainTexture = blueTexture;
}
if (i==3 zones[i]== 2)
{
zone4.renderer.material.mainTexture = redTexture;
}
if (i==3 zones[i]== 3)
{
zone4.renderer.material.mainTexture = greenTexture;
}
if (i==3 zones[i]== 4)
{
zone4.renderer.material.mainTexture = yellowTexture;
}
//remove each time from array - so we dont get repeat values.
arr.RemoveAt(index);
}
}