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
1
Question by oliver-jones · May 26, 2012 at 07:27 PM · arrayobjectstringintconvert

Array - Convert Object into Int

I seem to be really struggling here? I cant find a solution on google... unless I'm missing something?

I have an Object Array called playerKills, and I want to add 1 as an int to it (the array only holds Ints):

 var playerKills = new Array();

 //Expression 'self.playerKills.get_Item(i) cannot be assigned to
 playerKills[0] ++;
 
 //Just keeps adding '1' to the end of the value
 playerKills[0] = "" + 1;
 
 //Ambiguous reference 'parseInt' .....
 var arrayValue = parseInt(playerKills[i]);
 var newValue = arrayValue + 1;
 playerKills[i] = "" + newValue;

What am I doing wrong here?

Ollie

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

5 Replies

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

Answer by whydoidoit · May 26, 2012 at 07:45 PM

var kills : int = playerKills[0]; playerKills[0] = kills + 1;

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 Simon Says · Apr 06, 2013 at 12:41 AM 0
Share

Warning: implicit downcast from Object to int.

There seems to be no 'clean' way for automatic unboxing in UnityScript (though player$$anonymous$$ills[0] = 1; int kills = player$$anonymous$$ills[0]; should work fine in C#). And besides it's costly.

I think the only proper way to handle this is to really use generic collections as hinted by Eric5h5 before. The above mentioned solution looks more like a hack.

avatar image Eric5h5 · Apr 06, 2013 at 12:46 AM 0
Share

You could use parseInt to avoid that, but indeed it's really better not to use Array at all. It has no advantages, only disadvantages.

avatar image Simon Says · Apr 06, 2013 at 12:57 AM 0
Share

@Eric5h5: Well, the only way to do that would be something like:

player$$anonymous$$ills[0] = parseInt(player$$anonymous$$ills[0].ToString()) + 1;

And that's a hell of a hack, since you would go through several conversions just to get a simple value. No, I wouldn't even mention that. parse*() functions are for strings and there are no strings in the example (and shouldn't be). Hell has a special corner for the programmers who use such tricks ;-)

avatar image Eric5h5 · Apr 06, 2013 at 01:09 AM 0
Share

parseInt is for more than just strings, but indeed it doesn't parse Object so you'd have to cast the array value to something first, which sort of defeats the purpose I had in $$anonymous$$d....

avatar image
0

Answer by Eric5h5 · May 26, 2012 at 07:42 PM

Never use Array. You can use ++ with a built-in array of ints, but not List; in that case just add 1 the usual way.

 import System.Collections.Generic;
 
 function Start () {
     var foo = new int[1];
     foo[0]++;
     
     var foo2 = new List.<int>();
     foo2.Add(0);
     foo2[0] = foo2[0] + 1;
 }
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 ExTheSea · May 26, 2012 at 07:48 PM

Try:

 var playerKills = new Array();
 
 //Expression 'self.playerKills.get_Item(i) cannot be assigned to
 playerKills[0] ++;
 
 //Just keeps adding '1' to the end of the value
 playerKills[0] = playerKills[0] + 1;// I changed "" to playerKills[0] This adds 1 to the old value
 
 //Ambiguous reference 'parseInt' .....
 var arrayValue = parseInt(playerKills[i]);
 var newValue = arrayValue + 1;
 playerKills[i] = playerKills[i] + newValue; //I changed "" to playerKills[i] This adds newValue to the old value

I don't know if i really got your problem. That was just what i found a bit weird in your script.

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 Gaiyamato · Apr 06, 2013 at 01:47 AM

Why don't you just do this?

var playerKills:Array = new Array();

function AddPlayer():void { playerKills.Push(0); //make an instance of the int }

function AddKill(playernum:int):void { playerKills[playernum] = parseInt(playerKills[playernum].ToString())+1; //add 1 }

Or alternatively use a built-in.

var playerKills:int[] = new int[0];

function AddPlayer():void { var temp:int[] = new int[playerKills.length+1]; for(var i:int=0; i

Built-ins are faster but it depends on how many players you are adding in those code examples.

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 uxn · Aug 28, 2014 at 12:33 PM

This is .NET, so you can use System.Convert.ToInt32 instead of parseInt:

 playerKills[0] = System.Convert.ToInt32(playerKills[0]) + 1;

This way you can get rid of implicit downcast warnings.

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

10 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

Related Questions

Converting a string to an int 2 Answers

Convert Array to String 3 Answers

Why Won't This Work? 1 Answer

How to convert a string to int array in Unity C# 1 Answer

c# convert int to string 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