Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Ok so I did this the hard way...

Discussion in 'Scripting' started by numeric, Aug 29, 2010.

  1. numeric

    numeric

    Joined:
    Jan 31, 2010
    Posts:
    170
    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?

    Code (csharp):
    1. function Randomize()
    2.  
    3. {
    4.      var arr = new Array ();
    5.    
    6.     // Resize the array
    7.     arr.length = 4;
    8.    
    9.  
    10.     // Add data
    11.     // 1 = blue , 2= red, 3 = green, 4 = yellow
    12.     //each zone will be assigned a colour randomly.
    13.     arr[0] = 1;
    14.     arr[1] = 2;
    15.     arr[2] = 3;
    16.     arr[3] = 4;
    17.    
    18.     // iterate through the array to give each zone a random number(colour value).
    19.     for (var i = 0; i < 4; ++i)
    20.     {
    21.          var index = Random.Range(0,arr.length);
    22.         zones[i] = arr[index];
    23.        
    24.         ///     HANDLE ZONE 1 COLOURS ///  
    25.         /// i is just the zone-1 (due to the way arrays start at 0 )
    26.         //If zone one is currently selected in loop and colour 1 (blue) is choosen apply blue texture.
    27.         if (i==0  zones[i]== 1)
    28.         {
    29.             zone1.renderer.material.mainTexture = blueTexture;
    30.         }
    31.        
    32.         if (i==0  zones[i]== 2)
    33.         {
    34.             zone1.renderer.material.mainTexture = redTexture;
    35.         }  
    36.        
    37.         if (i==0  zones[i]== 3)
    38.         {
    39.             zone1.renderer.material.mainTexture = greenTexture;
    40.         }  
    41.        
    42.         if (i==0  zones[i]== 4)
    43.         {
    44.             zone1.renderer.material.mainTexture = yellowTexture;
    45.         }  
    46.        
    47.        
    48.         ///HANDLE ZONE 2 COLOURS///
    49.         ///i =1
    50.         if (i==1  zones[i]== 1)
    51.         {
    52.             zone2.renderer.material.mainTexture = blueTexture;
    53.         }
    54.        
    55.         if (i==1  zones[i]== 2)
    56.         {
    57.             zone2.renderer.material.mainTexture = redTexture;
    58.         }  
    59.        
    60.         if (i==1  zones[i]== 3)
    61.         {
    62.             zone2.renderer.material.mainTexture = greenTexture;
    63.         }  
    64.        
    65.         if (i==1  zones[i]== 4)
    66.         {
    67.             zone2.renderer.material.mainTexture = yellowTexture;
    68.         }
    69.        
    70.         /// HANDLE ZONE 3 COLOURS
    71.         ///i==2
    72.        
    73.         if (i==2  zones[i]== 1)
    74.         {
    75.             zone3.renderer.material.mainTexture = blueTexture;
    76.         }
    77.        
    78.         if (i==2  zones[i]== 2)
    79.         {
    80.             zone3.renderer.material.mainTexture = redTexture;
    81.         }  
    82.        
    83.         if (i==2  zones[i]== 3)
    84.         {
    85.             zone3.renderer.material.mainTexture = greenTexture;
    86.         }  
    87.        
    88.         if (i==2  zones[i]== 4)
    89.         {
    90.             zone3.renderer.material.mainTexture = yellowTexture;
    91.         }
    92.        
    93.         ///HANDLE ZONE 4 COLOURS
    94.         /// i ==3
    95.        
    96.         if (i==3  zones[i]== 1)
    97.         {
    98.             zone4.renderer.material.mainTexture = blueTexture;
    99.         }
    100.        
    101.         if (i==3  zones[i]== 2)
    102.         {
    103.             zone4.renderer.material.mainTexture = redTexture;
    104.         }  
    105.        
    106.         if (i==3  zones[i]== 3)
    107.         {
    108.             zone4.renderer.material.mainTexture = greenTexture;
    109.         }  
    110.        
    111.         if (i==3  zones[i]== 4)
    112.         {
    113.             zone4.renderer.material.mainTexture = yellowTexture;
    114.         }
    115.        
    116.        
    117.                
    118.         //remove each time from array - so we dont get repeat values.
    119.         arr.RemoveAt(index);  
    120.     }  
    121.  
    122. }  
    123.  
     
  2. rduclos

    rduclos

    Joined:
    Aug 26, 2010
    Posts:
    13
    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.

    Code (csharp):
    1. var zonesArr[]
    2. for (var i = 0; i < 4; ++i)
    3.     {
    4.         zoneColor = Random.Range(1,5);
    5.         if(zoneColor == 1)
    6.             zonesArr[i].renderer.material.mainTexture = blueTexture;
    7.         if(zoneColor == 2)
    8.             zonesArr[i].renderer.material.mainTexture = redTexture;
    9.         if(zoneColor == 3)
    10.             zonesArr[i].renderer.material.mainTexture = greenTexture;
    11.         if(zoneColor == 4)                                  
    12.             zonesArr[i].renderer.material.mainTexture = yellowTexture;
    13. }
    14.  
     
  3. menneske

    menneske

    Joined:
    Jan 23, 2009
    Posts:
    44
    If it ain't broken, don't try to fix it ;-)

    Other than that, if you put the zones and textures in arrays as well, it cleans up pretty nicely:
    Code (csharp):
    1.  
    2. // Sort function curtesy of google
    3. function fisherYates ( myArray ) {
    4.   var i = myArray.length;
    5.   if ( i == 0 ) return false;
    6.   while ( --i ) {
    7.      var j = Mathf.Floor( Random.value * ( i + 1 ) );
    8.      var tempi = myArray[i];
    9.      var tempj = myArray[j];
    10.      myArray[i] = tempj;
    11.      myArray[j] = tempi;
    12.    }
    13. }
    14.  
    15. var arr : Array = new Array(1,2,3,4);
    16. var texArr = new Array(blueTexture,greenTexture,redTexture,yellowTexture);
    17. var zoneArr = new Array(zone1,zone2,zone3,zone4);  
    18. function Randomize() {
    19.     // Randomize the indexing array
    20.     fisherYates(arr);
    21.    
    22.     // Distribute the textures (using the randomized index)
    23.     for( i=0; i<texArr.length; i++ )
    24.     {
    25.         zoneArr[i].renderer.material.mainTexture = texArr[arr[i]];
    26.     }
    27. }
    28.