Ok so I did this the hard way...

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);  
	}	

}

This is going off the fact though that Random.Range is going to return a different number each time through the four loop, Otherwise you may get a zone with the same color.

var zonesArr[]
for (var i = 0; i < 4; ++i) 
    { 
        zoneColor = Random.Range(1,5);
        if(zoneColor == 1)
            zonesArr[i].renderer.material.mainTexture = blueTexture; 
        if(zoneColor == 2)
            zonesArr[i].renderer.material.mainTexture = redTexture; 
        if(zoneColor == 3)
            zonesArr[i].renderer.material.mainTexture = greenTexture; 
        if(zoneColor == 4)                                  
            zonesArr[i].renderer.material.mainTexture = yellowTexture; 
}

If it ain’t broken, don’t try to fix it :wink:

Other than that, if you put the zones and textures in arrays as well, it cleans up pretty nicely:

// Sort function curtesy of google
function fisherYates ( myArray ) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Mathf.Floor( Random.value * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}

var arr : Array = new Array(1,2,3,4);
var texArr = new Array(blueTexture,greenTexture,redTexture,yellowTexture);
var zoneArr = new Array(zone1,zone2,zone3,zone4);   
function Randomize() {
	// Randomize the indexing array
	fisherYates(arr);
	
	// Distribute the textures (using the randomized index)
    for( i=0; i<texArr.length; i++ )
    {
        zoneArr[i].renderer.material.mainTexture = texArr[arr[i]];
    }
}