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 RevCosmosis · May 22, 2013 at 09:03 PM · androidjavascriptarraybce0051

Converting Array Object to int (or finding an alternative)

Using Javascript and developing for Android.

I'm using arrays to create a resizable list of coordinates that will describe a path that the player will draw on a 6x6 grid. While I could technically rewrite the code to use static arrays, I would like to see if I can get resizable arrays working.

Here's the code:

     var pathX = new Array();
     var pathY = new Array();
     
     function Start () {
     
         pathX.Add(9);
         pathY.Add(9);

         //I cut a bunch of stuff out here that's irrelevant
     }
                
     function InPath (xx, yy) {
         for (var i = 0; i < pathX.length; i++) {
             if (pathX[i] == xx && pathY[i] == yy) {
                 return true;
             }
         }
     }
     
     function NearPath (xx : int, yy : int) {
         if (pathX[pathX.length-1] == xx) {
             if (Mathf.Abs(pathY[pathY.length-1] - yy) <= 1) {
                 return true;
             }
         } else if (pathY[pathX.length-1] == yy) {
             if (Mathf.Abs(pathX[pathX.length-1] - xx) <= 1) {
                 return true;
             }
         } else {return false;}
     }

It returns this error:

Assets/LevelControl.js(68,53): BCE0051: Operator '-' cannot be used with a left hand side of type 'Object' and a right hand side of type 'int'.

Because the array class stores everything as object types, it can't do the subtraction. Is there a way to make this work with arrays, or should I just convert over to a static array?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by hoy_smallfry · May 22, 2013 at 09:11 PM

There's two ways this can be handled.

1) use System.Convert.ToInt32(System.Object obj) before you use the subtraction operator:

 var x : System.Object = 100;
 var y = System.Convert.ToInt32(x) ;

 Debug.Log("Type: " + x.GetType()); // should now be an System.Int32 aka int
 Debug.Log("Value: " + x);

 var z = y - 50;

2) Use List.< int > instead of Array, which will avoid conversion altogether.

 // goes up at the top of the script file
 import System.Collections.Generic;

 //...

 var list : List.<int> = new List.<int>();
 
 list.Add(100);
 
 Debug.Log("Type: " + list[0].GetType()); // should be an System.Int32 aka int
 Debug.Log("Value: " + list[0]);

 var z = list[0] - 50;
Comment
Add comment · Show 4 · 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 RevCosmosis · May 22, 2013 at 10:17 PM 1
Share

Thanks, lists worked! Is there any documentation I can find on lists?

avatar image Eric5h5 · May 22, 2013 at 10:18 PM 0
Share

Look on the $$anonymous$$SDN site.

avatar image RevCosmosis · May 22, 2013 at 10:43 PM 1
Share

Thanks again!

avatar image Julien-Lynge · May 22, 2013 at 11:16 PM 0
Share
  • for thanking people - it's a lost art :)

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

Is there a decent tutorial for a local high score table (android)? 1 Answer

Error when debuging array elements name 1 Answer

Building on android. 1 Answer

Sorting arrays? 1 Answer

How to delete new'd arrays? 2 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