Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BlackShadow · Jun 11, 2013 at 06:02 AM · javascript

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();
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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++)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Jun 11, 2013 at 06:13 AM 2
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Compiler errors need help with. Please 1 Answer

I need help with my Door Switch Configuration. 1 Answer

OnTriggerEnter Problem 3 Answers

syntax errors 1 Answer

Two problems with my worm script. 1)Not rotating when he gets hit and i am getting a turret error. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges