- Home /
How can I instantiate gameobjects from an array in a specific order?
Hey guys,
I know this should be rather simple, but for some reason I'm having a brain block. I just want to create an array, fill it with 3 objects, then instantiate them in order (0,1,2). So whichever object hits a platform first, instantiate 0, then second item hits platform, instantiate 1, then 2. Hope that makes sense. The simpler the better.
Answer by RodrigoAbreu · Feb 10, 2021 at 02:02 AM
Something like this would instantiate in order already:
public class Spawner : MonoBehaviour
{
public List<GameObject> prefabsToInstantiate;
void Start()
{
if (prefabsToInstantiate != null)
{
foreach(var obj in prefabsToInstantiate)
{
Instantiate(obj, transform.position, Quaternion.identity);
}
}
}
}
PS.: I just wrote in on a text file, it wasn't in Unity, so it's not tested for typos etc...
Your answer
![](https://koobas.hobune.stream/wayback/20220613024909im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate specific object from "FindGameObjectsWithTag" Array 0 Answers
IndexOutOfRangeException: Array index is out of range when using an Array and instantiating 2 Answers
Array with game object: object reference not set to an instance of an object 1 Answer
Instantiating an array of objects - how can I instantiate a certain prefab only once? 1 Answer