- Home /
Javascript / Unityscript adding elements of an array
A basic but vital question.
I have dice that spit out a number to an array that's a public var in a 'master' script.
var scoreArray = new Array();
if I do the general javascript means to add the items together in the array…
for (var i=0; i<scoreArray.length; i++){ // for every item in the array
total += scoreArray[i]; // stack-add them together in the variable total
}
I get an operator error in the console telling me that I can't mix the int variable total to the object elements of the array. I have read that Javascript / Unityscript arrays are objects, and I am using .push in the other dice scripts to append to the array and the unity reference manual says that these should, technically, be int additions rather than .add would be.
So what's the trick? I'm really not into the idea of recoding everything to C# at this point and I'd also like to know for future reference. Putting items in an array as objects sounds great in organizing GameObjects and the like, but if I'm just trying to tally a score, what should I do?
Thanks in advance!
Answer by Eric5h5 · Dec 10, 2012 at 10:26 PM
Don't use the JS Array class, use generic List.
import System.Collections.Generic;
var scoreArray : List.<int>;
Then:
for (var i = 0; i < scoreArray.Count; i++){
total += scoreArray[i];
}
thanks! I had read elsewhere that Javascript arrays are rather deprecated and pointless at this point. Is it better to orient oneself to using C# lists rather than Javascript lists, as well?
They aren't C# lists; a List is a List regardless of language. They are .NET Lists (or rather $$anonymous$$ono Lists, since scripting in Unity is built on $$anonymous$$ono). All collection types are the same in all languages, aside from the JS Array class which is specific to Unity and can only be used in Unityscript. http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F (Although there are more, such as Stack, Queue, and LinkedList; see the $$anonymous$$SDN docs.)
I've seen this kind of Array/builtinarray/List issue creep up from time to time. Perhaps there is some place in the docs or tutorials where we should tidy up to avoid $$anonymous$$ching people using Array? @Riontamer, it would be nice if you told us where/how you got into the habit of using Array so we can see if there are any improvements to make.
For me, creating an array simply seemed appropriate considering the project I worked on before this was a Shuffle Bag script. The script involves generating two arrays, one of 0's and one of 1's, with a randomly moving cursor that 'picks' and 'removes' each item it lands on. When the bag is empty it refills with the same amount as configured before based on modifications. However, I will say that in looking for info on that subject, I found an article, like I had mentioned above, calling Array's 'deprecated and slow' but thought mostly of it being a performance thing than a getting-it-to-work thing.
It would be nice if the docs for the JS Array class mentioned Lists. In fact the docs could stand to stress .NET/$$anonymous$$ono a lot more than they do, since newcomers tend to be either unaware of it entirely or think of it as a "C# thing", which is too bad since that's obviously a huge amount of functionality they're missing.
Answer by DeveshPandey · Dec 11, 2012 at 02:25 PM
Have you tried this?
var total:Number = 0;
for (var i=0; i<scoreArray.length; i++){ // for every item in the array
total += Number(scoreArray[i]); // stack-add them together in the variable total
}