How to instantiate multiple Prefabs in line and then repeat it?
// So, i have 3 prefabs. Each has 5 different gameobjects in it. How do i spawn them back to back and then loop them? //
// Like,... [spawn 5 objects from Prefab1 followed by 5 objects from Prefab2 and the same for the last Prefab3.] //
// Sorry, i don't have a clear code for reference. Just trying new things by following tutorials. And no, i don't have any experience in coding. //
Answer by highpockets · Nov 29, 2020 at 09:52 PM
Something like the below, but you have to work in your logic. Not sure if you have spawn positions/time between spawning, etc.. But this allows you to set your desired amount of prefabs and their child prefabs in the inspector (It assumes all children are the same prefab for each of the parents). It loops through the prefabs you set, spawns them, and then spawns the given children and sets the said prefab as the parent.
[SerializeField] GameObject[] prefabs;
[SerializeField] GameObject[] children;
private void Start()
{
for(int i = 0; i < prefabs.length; i++)
{
GameObject obj = Instantiate(prefabs[i]);
for(int j = 0; j < children; j++)
{
Instantiate(children[j], obj.transform);
}
}
}
Follow this Question
Related Questions
Instantiation of my GameObjects spell (from other script) 0 Answers
How to edit new clone properties? 1 Answer
Instantiate a prefab at positions on a list 0 Answers
My gameObject (enemy) is being spawned rapidly to the point of crashing, how do I fix this? 0 Answers
*Solved* Loop instantiating objects causes my Unity Editor to crash 1 Answer