- Home /
Transform array help
My original post was closed before I could ask this so I am creating a new one. http://answers.unity3d.com/questions/464315/how-to-create-an-array-of-transforms.html
I tried using several of the suggestions and am having issues. Any insight in to what I am doing wrong would be greatly appreciated.
I tried using:
var _targets : Transform[];
But when I tried to drag and drop my corner game objects on to the elements in the inspector it will not take them. In other words I have the AI script on my enemy game object prefab. It has the AI script on it which has the variable _targets. I adjust the target size to 4. Then try to drag and drop the corner gameobjects on it.
How do I drag and drop the transform of a game object on to this in the inspector?
I also tried to do this manually in the script. I tried using.
var balls : ArrayList;
function Start()
{
balls = new ArrayList();
}
function Update()
{
cornerOne= GameObject.FindWithTag ("corner1");
cornerTwo= GameObject.FindWithTag ("corner2");
cornerThree= GameObject.FindWithTag ("corner3");
cornerFour= GameObject.FindWithTag ("corner4");
corner1 = cornerOne.transform;
corner2 = cornerTwo.transform;
corner3 = cornerThree.transform;
corner4 = cornerFour.transform;
balls.Add(corner1);
balls.Add(corner2);
balls.Add(corner3);
balls.Add(corner4);
}
However when I try to print whats in my array called balls. It just says "System.Collections.ArrayList". It does not list any of the transforms I added to the array.
I am sure I have the context wrong but I am at a loss for where I am going wrong..
Have you tried ragging it on the name, not the length variable? Because that definitely works!!!
Answer by robertbu · May 28, 2013 at 02:48 AM
Assuming you know the size ahead of time the size will be 4, you can do:
private var balls : Transform[];
function Start() {
balls = new Transform[4];
}
function Update() {
balls[0] = GameObject.FindWithTag ("corner1").transform;
balls[1] = GameObject.FindWithTag ("corner2").transform;
balls[2] = GameObject.FindWithTag ("corner3").transform;
balls[3] = GameObject.FindWithTag ("corner4").transform;
}
If the number of entries is variable, consider using a List (Generic), not an ArrayList.
Reference on array types:
http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F
Your answer
Follow this Question
Related Questions
Instantiate from array into array? 2 Answers
How To Get A Reference To All Nearby GameObjects? 1 Answer
Problems with Arrays 2 Answers
Array.length edited using scripts 2 Answers
How to Narrow Down an Array casted from FindObjectsOfType 1 Answer