Search Unity

Multiple Arrays

Discussion in 'Scripting' started by ctsteve123, Jul 15, 2008.

  1. ctsteve123

    ctsteve123

    Joined:
    Sep 13, 2007
    Posts:
    51
    Multiple Array:


    Code (csharp):
    1. private var rowArray:Array;
    2.  
    3. function Start () {
    4.    
    5.     rowArray  = [[ 0 , 1 , 0 , 0 , 1],[ 0 , 1 , 0 , 0 , 1],[ 0 , 1 , 0 , 0 , 1] ];
    6.    
    7.     var i:int = 0;
    8.     var j:int = 0;
    9.     var rowLength:int = rowArray.length;
    10.     var colLength:int = rowArray[0].length;
    11.  
    12.     for( i ; i < rowLength ; i++ ){
    13.        
    14.         for( j ; j < colLength ; j++ ) {
    15.            
    16.              if ( rowArray[i][j] ){
    17.                
    18.                 //do something
    19.                
    20.              }//end if
    21.            
    22.         }//end for
    23.          
    24.     }//end for
    25. }

    I am getting the following error " Expressions in statements must only be executed for their side effect?" for the second "for" statement. Plus some other errors.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. private var rowArray:Array;
    2.  
    3. function Start () {
    4.    
    5.    rowArray  = [[ 0 , 1 , 0 , 0 , 1],[ 0 , 1 , 0 , 0 , 1],[ 0 , 1 , 0 , 0 , 1] ];
    6.    
    7.    var rowLength = rowArray.length;
    8.    var colLength = rowArray[0].Length;
    9.  
    10.     for( var i = 0; i < rowLength ; i++ ){
    11.        
    12.         for( var j = 0; j < colLength ; j++ ) {
    13.              
    14.             if ( rowArray[i][j] ){
    15.                
    16.                 //do something
    17.                
    18.             }//end if
    19.            
    20.        }//end for
    21.        
    22.     }//end for
    23. }
    You can't do things like "for (i; i < rowLength"...you have to set i to something explicitly. There's no point in declaring the variables beforehand since the scope is function-wide (one thing about Unityscript that actually is like Javascript ;) ). Note the difference between "length" and "Length". Because rowArray is a dynamic array which uses "length", but it contains a series of int[] built-in arrays, which use "Length". Yes, that's a bit weird.... Also, there's no need to explicitly type the variables here if you don't want to; Unity takes care of that for you with type inference (not the same thing as dynamic typing). Not that it hurts to do so of course.

    --Eric
     
  3. ctsteve123

    ctsteve123

    Joined:
    Sep 13, 2007
    Posts:
    51
    I changed the .Length too .length and the same problem.

    But when I set the i=0 and j=0 inside the for loop everything was fine .... That is odd.

    Code (csharp):
    1. var i ;
    2.     var j ;
    3.     var rowLength  = rowArray.length;
    4.     var colLength  = rowArray.length;
    5.  
    6.     for( i = 0 ; i < rowLength ; i++ ){
    7.            
    8.           for( j = 0 ; j < colLength ; j++ ) {
    9.            
    10.               if ( rowArray[i][j] ){
    11.                
    12.                 var wall: GameObject = Instantiate(  wallSection, Vector3( 0,0,i) , transform.rotation );
    13.                 wall.transform.parent = this.transform;
    14.                
    15.               }//end if
    16.            
    17.           }//end for
    18.          
    19.      }//end for
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. var i ;
    2.     var j ;
    You don't want to do that...that makes i and j dynamically typed, which makes them much slower than integers. You don't need to declare them beforehand at all.

    Code (csharp):
    1.    var rowLength  = rowArray.length;
    2.     var colLength  = rowArray.length;
    You've set rowLength and colLength to the same thing. Use the code I posted...it works fine. ;)

    --Eric
     
  5. ctsteve123

    ctsteve123

    Joined:
    Sep 13, 2007
    Posts:
    51
    You are right thanks Eric5h5 for the help! I didn't see that you had changed the code.
     
  6. rumliege dan

    rumliege dan

    Joined:
    Apr 19, 2008
    Posts:
    30
    do this:

    Code (csharp):
    1.  
    2. private var rowArray : Array;
    3.  
    4. function Start () {
    5.    
    6.    rowArray  = [[ 0 , 1 , 0 , 0 , 1],[ 0 , 1 , 0 , 0 , 1],[ 0 , 1 , 0 , 0 , 1] ];
    7.    for (var i : int = 0; i < rowArray.length; i++) {
    8.       var tarr : Array = rowArray[i];
    9.       for (var j : int = 0; j < tarr.length; j++) {
    10.          print (tarr[j]);
    11.       }
    12.    }
    13. }
    14.