- Home /
ios cant access variables in array
var arr = new Array()
var i : int=0;
var temp : String;
for(i=0;i<animationSlotsUsed;i++){
temp = arr[i];
}
this for some reason dosent work for me, im using pragma strinct so i can compile on ios. any ideas what is wrong?
oh also i cut out the part populating the array with strings
What errors or warnings, if any, do you get? It helps if we know what is actually happening. Does it just not assign anything to temp? Are you certain animationSlotUsed isn't 0? Then the loop wont run at all.
also remember ; at the end of EVERY code line. Eg. after Array()
perhaps you could declare the array as type of array too?
var arr : Array = new Array();
the error is cant convert type 'object' to 'string' could it be that i have to set the array to type string?
That sounds like it. Otherwise arr is an unknown aka object
Answer by jahroy · Oct 12, 2011 at 11:09 PM
You are trying to use a dynamic (untyped) array.
There are two types of arrays in Unity: dynamic arrays and builtin arrays.
I would recommend using a builtin array.
You can convert a dynamic array to a builtin array like this:
/* declare dynamic array */
var dynamicArray = new Array();
/ populate dynamic array with ints /
for ( var i = 0; i < 10; i ++ ) { dynamicArray.Push(i); }
/ convert to builtin array of known type /
var builtinIntArray = dynamicArray.ToBuiltin(int);
Also... unless there is something missing from your code, you never populate the array named arr.
Your code creates a new, empty, untyped array. It never puts any data in the array (unless there is something you're not showing us).
yes i took out most of the code, it all works well im just transfering scripts over to pragma strict for ios. that looks helpful i will try it out. also do you need to declare the builtin array? to say type string in my case?