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 Xzauhst · Aug 05, 2013 at 04:04 AM · androidjavascripterror messageweb

Writing javascript for web and android

I'm getting multiple errors when I try to go from building a web to building for android. My program runs fine when it's in web mode, but for android it's giving me

Assets/Scripts/GameScript.js(26,13): BCE0005: Unknown identifier: 'i'

The error in the code below is actually on line 24, because it deletes the spaces at the top of my program when I copy it to here

Here is my code

 //variables
 var cols:int = 4; //the number of columns in the card grid
 var rows:int = 4; //the number of rows in the card grid
 var totalCards:int = cols * rows; //the number of cards in the game
 var matchesNeededToWin:int = totalCards * 0.5; //if there are 16 cards, the player needs 8 matches to clear the board
 var matchesMade:int = 0; //starting amount of matches made
 var cardW:int = 100; //cards width and height
 var cardH:int = 100;
 var aCards:Array; //store all the cards made in this array
 var aGrid:Array; //this array will keep track of the shuffled, dealt cards
 var aCardsFlipped:ArrayList; //store the two cards that the player flips over
 var playerCanClick:boolean; //flag this boolean to prevent the player from pressing buttons when we don't want them to
 var playerHasWon:boolean = false; //store whether or not the player has won. Should probably start out false :)
 
 function Start () 
 {
     playerCanClick = true; //we should let the player play, shouldn't we?
     
     //initialize the arrays as empty lists
      var aCards = new Array();
      var aGrid = new Array();
      var aCardsFlipped = new ArrayList();
     
 /* -->this is where the error is*/    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 OnGUI ()
 {
  GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));
  BuildGrid(); 
  GUILayout.EndArea();
 }
 
 class Card extends System.Object 
 {
     
     var isFaceUp:boolean = false;
     var isMatched:boolean = false;
     var img:String;
     
     function Card()
     {
         img = "robot";
     }
     
 }
 
 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 · Show 4
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 Eric5h5 · Aug 05, 2013 at 05:25 AM 0
Share

Always leave #pragma strict at the top of all your scripts, so you don't end up with any surprises when switching platforms like that.

Also, never use the JS Array class, and never use ArrayLists either. Always use typed collections such as built-in arrays or generic Lists.

avatar image Xzauhst · Aug 05, 2013 at 05:40 AM 0
Share

O$$anonymous$$ that solved my unknown identifier problem, and I also included #pragma strict so i can get used to it since I'll be program$$anonymous$$g for mainly android. But now I get this error:

Assets/Scripts/GameScript.js(66,48): BCE0048: Type 'Object' does not support slicing.

Here is my code now. I put an arrow as I did before so it may be easier to see it. Thanks for the help.

     #pragma strict
     
     //variables
     var cols:int = 4; //the number of columns in the card grid
     var rows:int = 4; //the number of rows in the card grid
     var totalCards:int = cols * rows; //the number of cards in the game
     var matchesNeededToWin:int = totalCards * 0.5; //if there are 16 cards, the player needs 8 matches to clear the board
     var matches$$anonymous$$ade:int = 0; //starting amount of matches made
     var cardW:int = 100; //cards width and height
     var cardH:int = 100;
     var aCards:Array; //store all the cards made in this array
     var aGrid:Array; //this array will keep track of the shuffled, dealt cards
     var aCardsFlipped:ArrayList; //store the two cards that the player flips over
     var playerCanClick:boolean; //flag this boolean to prevent the player from pressing buttons when we don't want them to
     var playerHasWon:boolean = false; //store whether or not the player has won. Should probably start out false :)
     
     function Start () 
     {
         playerCanClick = true; //we should let the player play, shouldn't we?
         
         //initialize the arrays as empty lists
          var aCards = new Array();
          var aGrid = new Array();
          var aCardsFlipped = new ArrayList();
         
         for(var i=0; i<rows; i++)
         {
              aGrid[i] = new Array(); //create a new, empty array at index i
             
             for(var j=0; j<cols; j++)
             {
                 (aGrid[i] as Array)[j] = new Card();
             }
         }
     }
     
     function OnGUI ()
     {
      GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));
      BuildGrid(); 
      GUILayout.EndArea();
     }
     
     class Card extends System.Object 
     {
         
         var isFaceUp:boolean = false;
         var is$$anonymous$$atched:boolean = false;
         var img:String;
         
         function Card()
         {
             img = "robot";
         }
         
     }
     
     function BuildGrid()
     {
         GUILayout.BeginVertical();
         for(var i=0; i<rows; i++)
         {
             GUILayout.BeginHorizontal();
             for(var j=0; j<cols; j++)
             {
   error is right here -->  var card:Object = aGrid[i][j];
                 if(GUILayout.Button(Resources.Load(card.img), GUILayout.Width(cardW)))
                 {
                     Debug.Log(card.img);
                 }
                 
             }
             GUILayout.EndHorizontal();
         }
         GUILayout.EndVertical();
     }
 
avatar image Eric5h5 · Aug 05, 2013 at 06:12 AM 0
Share

Like I said, don't use Array or ArrayList, use a generic List. Also don't use Object as a type, use the actual specific type.

avatar image Xzauhst · Aug 05, 2013 at 06:18 PM 0
Share

Can you give me an example? I did some googleing but can't get my program to work when trying different examples.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Aug 05, 2013 at 04:26 AM

It seems that #pragma strict is automatically set when compiling for Android, thus you must explicitly declare all variables you use. You must also define the variable type either explicitly or by initializing it with a constant of known type. In this case, you should modify the for statement:

 for(var i=0; i<rows; i++) // declare i...
 {
     aGrid[i] = new Array(); //create a new, empty array at index i
 
    for(var j=0; j<cols; j++) // and declare j as well!
    {
       aGrid[i][j] = new Card();
    }
 }

Remember to declare all variables used in your code, like in the other for statements.

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

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

16 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

Related Questions

A node in a childnode? 1 Answer

Make an object move in the direction of touch 0 Answers

opening a webpage? 1 Answer

using Application.OpenURL for opening new tab? 2 Answers

Accelerator and how to limit player movement (Android) 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