- Home /
Add GameObject to an Array.
I'm trying to add Specific game objects and their position to an array so they can be Instantiated afterwards in their positions? I know how to add the gameobjects to an Array but not their positions. Anyone got any clues?
public GameObject[] Objects;
public GameObject Ball;
void Update() [
if(Input.GetKeyDown(KeyCode.Space)) {
Objects.Add(Ball);
}
else if (Input.GetKeyDown(KeyCode.Enter)) {
Instantiate(Objects, transform.position, transform.rotation);
}
}
Answer by Halfbiscuit · May 18, 2015 at 11:17 AM
Well first of all Arrays do not have a .Add function, for that you need a List:
Also you cannot instantiate an entire array or list you will need a for loop to instantiate each object individually.
A list/array for the positions is all you would actually need as the Ball variable is being passed into the Instantiate function every time and will never change.
Your answer

Follow this Question
Related Questions
Creating a 2d array with the Array class 1 Answer
Obtaining an array of positions from an array of gameobjects 2 Answers
Adding a texture to array textures?! 1 Answer
Creating and loading in Array Presets? 0 Answers
Storing objects in arrays 2 Answers