- Home /
Adding value to GameObject[]
I have a GameObject[] and I tried to add value to it with Add(), but it says " a definition for Array' and no extension method
Array' of type `UnityEngine.GameObject[]' could be found". How do I do it?
Add()
is used for List
s... use that ins$$anonymous$$d of an array.
List<GameObject> _myGameObjects = new List<GameObject>();
...
_myGameObjects.Add(someGameObject);
add what value to it? you need to reference a script within a gameobject if you want to modify that
Answer by nyonge · Oct 18, 2014 at 05:19 PM
Arrays are fixed size at creation, you might wanna use a List. But if you wanna stick with arrays:
Assuming you're using C#, here's a function I wrote awhile back that lets you add a GameObject to a GameObject[] array. You pass in the original array and the gameobject, and it returns a new array with your gameobject added to it.
//adds GameObject Obj to array Array
public static GameObject[] AddToGameObjectArray(GameObject[] Array,GameObject Obj) {
GameObject[] NewArray = new GameObject[Array.Length + 1];
for (int i = 0; i <= Array.Length; i++) {
if (i == Array.Length)
NewArray[i] = Obj;
else
NewArray[i] = Array[i];
}
return NewArray;
}
Your answer
Follow this Question
Related Questions
I can do this in JS or not ??? 2 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
"NullReferenceException" while trying to draw a texture (C#) 1 Answer