How do I spawn an array of prefab clones as children of an object?
I'm currently using the following to spawn the "spawnClone" prefabs where they need to be and it is doing that just fine, however, I need them parented to "spawnLocations" and am unsure as to how.
using UnityEngine;
using System.Collections;
public class Structure_Spawner : MonoBehaviour {
public Transform[] spawnLocations;
public GameObject[] spawnPrefab;
public GameObject[] spawnClone;
void Start(){
spawnSomething();
}
void spawnSomething(){
spawnClone[0] = Instantiate(spawnPrefab[0], spawnLocations[0].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
spawnClone[1] = Instantiate(spawnPrefab[1], spawnLocations[1].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
spawnClone[2] = Instantiate(spawnPrefab[2], spawnLocations[2].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
spawnClone[3] = Instantiate(spawnPrefab[3], spawnLocations[3].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
spawnClone[4] = Instantiate(spawnPrefab[4], spawnLocations[4].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
spawnClone[5] = Instantiate(spawnPrefab[5], spawnLocations[5].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
spawnClone[6] = Instantiate(spawnPrefab[6], spawnLocations[6].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
spawnClone[7] = Instantiate(spawnPrefab[7], spawnLocations[7].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
}
}
Answer by TBruce · May 07, 2016 at 02:08 PM
This is a simple system that does what you are looking for but using Lists instead of Arrays (more efficient)
I am sorry that I missed that.
See my updated code below
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Structure_Spawner : MonoBehaviour
{
public int numberOfItemsToSpawn = 7; // modifiable in the inspector
public List<Transform> spawnLocations = new List<Transform>();
public List<GameObject> spawnPrefab = new List<GameObject>();
public List<GameObject> spawnClone = new List<GameObject>();
void Start()
{
for (int i = 0; i < numberOfItemsToSpawn; i++)
{
if ((i < spawnPrefab.Count) && (spawnPrefab[i] != null) &&
(i < spawnLocations.Count) && (spawnLocations[i] != null))
{
if (spawnLocations[i].parent != null)
{
// this line spawns a prefab a an objects position
spawnClone[i] = Instantiate(spawnPrefab[i], spawnLocations[i].parent.position, Quaternion.Euler(0, 0, 0)) as GameObject;
// this line makes the spawned prefab a child of the parent the prefab was spawned from
spawnClone[i].transform.SetParent(spawnLocations[i].parent, false);
}
}
else // this can else block can be removed
{
if (i >= spawnPrefab.Count)
Debug.Log("spawnPrefab.Count = " + spawnPrefab.Count + " - Trying to spawn item number " + (i + 1)); // i is zero based
else if (spawnPrefab[i] == null)
Debug.Log("spawnPrefab[" + i "] is null";
if (i >= spawnLocations.Count)
Debug.Log("spawnLocations.Count = " + spawnLocations.Count + " - Trying to spawn item number " + (i + 1)); // i is zero based
else if (spawnLocations[i] == null)
Debug.Log("spawnLocations[" + i "] is null";
}
}
}
}
What about parenting though? They spawn to the transforms assigned, but those transforms move and I need a way to attach the clones to them.
The parenting side works, however the "spawnClones" are parented to the object containing the script (rather their individually assigned "spawnLocations") and spawn at its location, rather than their assigned "spawnLocations".
Ins$$anonymous$$d of this
spawnClone[i].transform.SetParent(spawnLocations[i].parent, false);
do this
spawnClone[i].transform.SetParent(gameObject.transform, false);
Still doesn't work, but I've wasted enough of your time. Thank you for all your help.
Your answer
Follow this Question
Related Questions
Parenting objects with code not working 2 Answers
How to save as prefab when a button is pressed 1 Answer
Objects lose collision when set as child? 0 Answers
Moving grabbed object OnTriggerStay2D 0 Answers