- Home /
Declaring a type in JavaScript arrays
Hi. I need to use a multidimensional array to store information about certain GameObjects that i instantiate. Problem is that the dynamic JavaScript array turns everything into the type of Object. So when i call the function "Destroy()", i am told that i give it the wrong argument (an object).
//Storing the data in my array
footstepRotation[i] = (180/Mathf.PI)*(Mathf.Atan((currentPosition[i].z-lastPosition[i].z)/(currentPosition[i].x-lastPosition[i].x))); var spawnedFootstep : GameObject = Instantiate(footsteps,currentPosition[i], Quaternion.Euler(0,footstepRotation[i]-90,0)); footstepArray.Add(spawnedFootstep); footstepArrayArray[i] = footstepArray; lastPosition[i] = currentPosition[i];
//function to destroy the footsteps
Destroy(footstepArrayArray[0][0]);
Answer by SilverTabby · Sep 06, 2011 at 10:15 AM
DO NOT use the JavaScript Dyanimc arrays. I have never gotten them to work the way they are supposed to.
just create a multi-dimensional array like this:
var footstepArrayArray : GameObject[,] = new GameObject[length1,length2];
for(var X : int = 0; X < footstepArrayArray.GetLength(0); X++)
for(var Y : int = 0; Y < footstepArrayArray.GetLength(1); Y++)
footstepArrayArray[X,Y] = Instantiate(something);
A half fix so far - i need to have a dynamic array though - since i do not know how many footsteps i wish to instantiate. - how would you increase the length dynamically with the instantiation of steps?
Answer by SilverTabby · Sep 06, 2011 at 01:16 PM
If you footsteps do not serve a Gameplay purpose in any way, shape, or form; then you only need a static Array to hold them. Just Destroy() the existing object in the array before putting a new one there. Then when your index >= array.length; index = 0; This way you will be able to keep all your footsteps in an easy to use collection, and it will delete them automatically.
If you need a dynamically sized Collection (the footsteps ARE important to your game), you might want to try a List of GameObjects, or a List of Lists of GameObjects
import System.Collections.Generic.List;
var footsteps : List.<GameObject> = new List.<GameObject>();
//you can add bits to the List by hand, or you can
// pass an array or another collection into the
// constructor to have the contents of your new
// list = contents of the parameter.
footsteps.Add(Instantiate(something));
//you can use a for-each loop to go through a List
for(var step : GameObject in footsteps)
{ doSomething(step); }
//another way to iterate through this collection
var stepArr : GameObject[] = footsteps.ToArray();
for(var count : int = 0; count < stepArr.length; count++)
doSomething( stepArr[count] );
var listOfLists : List.<List.<GameObject>> = new List.<List.<GameObject>>();
var list : List.<GameObject> = new List.<GameObject>();
listOfLists.Add(list);
for(var l : List.<GameObject> in listOfLists)
for(var stp : GameObject in l)
doSomething(stp);
I've tried this code in Unity and it wont compile: var listOfLists : List.> = new List.>();
It keeps showing expecting > but found >>
Deleting the second > brings up a new error expecting > but found ;
Can someone tell me what is the correct way to declare a list of lists?
Ok found the answer, leave a space between then two end > >
Answer by aldonaletto · Sep 06, 2011 at 12:27 PM
As far as I know, the only way to typecast a value in Unityscript is to store the value in a variable of the correct type, like this:
var go: GameObject = footstepArrayArray[0][0]; Destroy(go);
It may solve the typecast problem, but I really don't know how you will sweep the arrays of arrays to delete each element - hope you know what you're doing!
I tried this kind of type casting though it didn't work.. I got the problem cleared by sorting the GameObjects by name ins$$anonymous$$d - a sneaky little fix - but atleast it works for now (:
Your answer
Follow this Question
Related Questions
Remove dead enemies from array 1 Answer
Using DestroyImmediate on Arrays? 1 Answer
How to destroy a unit contained in multiple lists 1 Answer
Variable resetting back to zero 2 Answers
Arrays declaration c# 3 Answers