Multiple Array: Code (csharp): private var rowArray:Array; function Start () { rowArray = [[ 0 , 1 , 0 , 0 , 1],[ 0 , 1 , 0 , 0 , 1],[ 0 , 1 , 0 , 0 , 1] ]; var i:int = 0; var j:int = 0; var rowLength:int = rowArray.length; var colLength:int = rowArray[0].length; for( i ; i < rowLength ; i++ ){ for( j ; j < colLength ; j++ ) { if ( rowArray[i][j] ){ //do something }//end if }//end for }//end for } 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.
Code (csharp): private var rowArray:Array; function Start () { rowArray = [[ 0 , 1 , 0 , 0 , 1],[ 0 , 1 , 0 , 0 , 1],[ 0 , 1 , 0 , 0 , 1] ]; var rowLength = rowArray.length; var colLength = rowArray[0].Length; for( var i = 0; i < rowLength ; i++ ){ for( var j = 0; j < colLength ; j++ ) { if ( rowArray[i][j] ){ //do something }//end if }//end for }//end for } 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
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): var i ; var j ; var rowLength = rowArray.length; var colLength = rowArray.length; for( i = 0 ; i < rowLength ; i++ ){ for( j = 0 ; j < colLength ; j++ ) { if ( rowArray[i][j] ){ var wall: GameObject = Instantiate( wallSection, Vector3( 0,0,i) , transform.rotation ); wall.transform.parent = this.transform; }//end if }//end for }//end for
Code (csharp): var i ; 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): var rowLength = rowArray.length; var colLength = rowArray.length; You've set rowLength and colLength to the same thing. Use the code I posted...it works fine. --Eric
do this: Code (csharp): private var rowArray : Array; function Start () { rowArray = [[ 0 , 1 , 0 , 0 , 1],[ 0 , 1 , 0 , 0 , 1],[ 0 , 1 , 0 , 0 , 1] ]; for (var i : int = 0; i < rowArray.length; i++) { var tarr : Array = rowArray[i]; for (var j : int = 0; j < tarr.length; j++) { print (tarr[j]); } } }