- Home /
Question by
Heke · Jun 27, 2013 at 07:32 PM ·
gameobjectlistarraystarget
How to add and access objects in array?
This is simple but couldn't find any answers. How do I add GameObjects to an array? isnt it just
var targets : GameObject[];
var oneTarget : GameObject;
targets = oneTarget;
and How to access one of the objects in the array, lets say the seconds one, I tried...
Instantiate(targets[2]);
Comment
Answer by Eric5h5 · Jun 27, 2013 at 07:40 PM
No, your code is attempting to assign a GameObject variable to a GameObject[] variable, which is impossible. In fact you cannot add anything to built-in arrays; you initialize them with a pre-set number of elements. This can be done in code, or in the inspector. If you want an array where the number of elements can be dynamically changed, use a generic List instead, then you can use List.Add.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to create an array of Transforms 1 Answer
Switch Case List of Game objects ?? 2 Answers