- Home /
Add GameObject to Array
obj is a GameObject. I'm getting an error trying to add it to my array see below:
var myArray = new Array();
myArray.Add(obj);
I've looked all over UnityAnswers - how can I add a GameObject to an array?
It's been asked a hundred times but none of the answers make any sense to me. if I want to use the array type above is it possible?
Answer by Seth-Bergman · Mar 21, 2013 at 06:18 AM
First of all, you shouldn't use the Array() class.. You're better off using a List
(if you do prefer it though, see here):
http://www.w3schools.com/js/js_obj_array.asp
rather than "Add" try "Push"
myArray.Push(obj);
for a list, you could say:
var myList = new List.<GameObject>();
myList.Add(obj);
Your answer
Follow this Question
Related Questions
New Object Pooling Problem 1 Answer
How to set to game objects's position from 2 different game objects arrays equal to each other? 0 Answers
Transport unknown amout of objects with GameObject array 0 Answers
View an array of the transform position/rotation of all game objects with a specified tag., 0 Answers
Spawning GameObjects with help of classes --- Attaching classes to GameObjects 1 Answer