- Home /
Unknown Identifier 'i'
I was scripting a gamescript and following the instructions of a beginner's guide to Unity, until I ran into a problem to which I cannot find a fix for.
for(i=0; i<rows; i++)
I will post the entire code if needed.
var cols:int = 4;
var rows:int = 4;
var totalCards:int = cols*rows;
var matchesNeededToWin:int = totalCards * 0.5;
var matchesMade:int = 0;
var cardW:int = 100;
var cardH:int = 100;
var aCards:Array;
var aGrid:Array;
var aCardsFlipped:ArrayList;
var playerCanClick:boolean;
var playerHasWon:boolean = false;
function Start () {
playerCanClick = true; // We should let the player play, don't you think? XD
//Initialize the arrays as empty lists:
aCards = new Array();
aGrid = new Array();
aCardsFlipped = new ArrayList();
for(i=0; i<rows; i++)
{
aGrid[i] = new Array(); // Create a new, empty array at index i
for(j=0; j<cols; j++)
{
aGrid[i][j] = new Card();
}
}
}
function Update () {
}
class Card extends System.Object
{
var isFaceUp:boolean = false;
var isMatched:boolean = false;
var img:String;
function Card()
{
img = "robot";
}
}
function OnGUI()
{
GUILayout.BeginArea (Rect (0, 0, Screen.width, Screen.height));
BuildGrid();
GUILayout.EndArea();
print("Building grid!");
}
function BuildGrid()
{
GUILayout.BeginVertical();
for(i=0; i<rows; i++)
{
GUILayout.BeginHorizontal();
for(j=0; j<cols; j++)
{
var card:Object = aGrid[i][j];
if(GUILayout.Button(Resources.Load(card.img),
GUILayout.Width(cardW)))
{
Debug.Log(card.img);
}
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
Answer by robertbu · Jun 11, 2013 at 06:04 AM
It should be:
for(var i=0; i<rows; i++)
Or if you need 'i' to live beyond the for() statement you can do:
var i : int;
for(i=0; i<rows; i++)
Answer by bubzy · Jun 11, 2013 at 06:05 AM
I don't know javascript but in c# you have to declare the variable BEFORE you start the loop so:
for(int i=0; =<rows; i++)
{
////blah
}
or (as fafase correctly stated)
int i = 0;
for (i=0;i<rows;i++)
{
//blah
}
without the int declaration, the program doesn't know what 'i' is. maybe you have to
var i
or something
That is not true. In C# you can do:
int i = 0;
for( ; i < Something; i++){}
Or even reuse the same variable for two loops:
int i = 0;
for( ; i < Something; i++){}
// I continue with the value of i left from the previous loop
for( ; i < Something+100; i++){}
Finally, var does work in C# as long as you provide a value that allows the compiler to infer the type.
var fl = 0.5f; // Same as float fl = 0.5f
var str = "A string"; // Same as string str = "A string";
Your answer