- Home /
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
Answer by whydoidoit · May 26, 2012 at 07:45 PM
var kills : int = playerKills[0]; playerKills[0] = kills + 1;
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.
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.
@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 ;-)
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....
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;
}
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.
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.
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.
Your answer
Follow this Question
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