- Home /
How to add multiple clones of an object to a list ? Currently only 1 clone gets added.
Only 1 of the multiple instantiated clones of an object is getting added to my list, instead of ALL of them. Also, is it possible to store all such clones under a single variable so I can access all of them from a different script ??
Here is my code:
public GameObject[] enemies; // Array of enemy prefabs.
List<GameObject> instantiatedCrowd;
public GameObject instantiatedEnemies;
void Start ()
{
// Start calling the Spawn function repeatedly after a delay .
InvokeRepeating("Spawn", spawnDelay, spawnTime);
}
void Spawn ()
{
// Instantiate a random enemy.
int enemyIndex = Random.Range(0, enemies.Length);
instantiatedEnemies = Instantiate(enemies[enemyIndex], transform.position, transform.rotation) as GameObject;
instantiatedCrowd.Add (instantiatedEnemies.gameObject);
}
How and where do you call the Spawn() function? If it only gets called once, it's just one random enemy spawning, right? Also if you want to loop through the whole enemy array, for a Random.Range based on integers does not include the 'max' property. So Random.Range(0, 5) returns a number between 0 and 4 (included) not 0 and 5.
Spawn() gets called under InvokeRepeating every few seconds. Enemy array has different types of enemies that spawn. Random.Range of 0 to enemies.length is basically asking it to choose a random item from the entire array, and has nothing to do with the list that I want each and every spawned enemy to get listed under.
$$anonymous$$anaged to get it to work with the following script, but it updates the list only when the Invoke is called and not every frame, as I would want it to be. I can't add the Instantiate command under Update() since I'm making a call to the Spawn() under the Invoke. Any ideas ?
public GameObject[] enemies; // Array of enemy prefabs.
public List <GameObject> instantiatedCrowd;
void Start ()
{
// Start calling the Spawn function repeatedly after a delay .
InvokeRepeating("Spawn", spawnDelay, spawnTime);
}
void Spawn ()
{
// Instantiate a random enemy.
int enemyIndex = Random.Range(0, enemies.Length);
instantiatedCrowd.Add(Instantiate(enemies[enemyIndex], transform.position, transform.rotation) as GameObject);
}
Why would you want the list to be updated every frame? If enemies are added only in Spawn()
, isn't it enough that the list is updated there? What do you want to do with the list instantiatedCrowd
?
I wish to check for bounds of all the objects inside the list so that I can check if those objects are intersecting with a different object, so as to trigger damage to player health.
Answer by coolbird22 · Apr 28, 2014 at 03:58 PM
Marking this solved. Answer includes most of the replies, with Benproductions1 answering my final query
Your answer
Follow this Question
Related Questions
Why does instantiation of clone cause duplicates 1 Answer
A node in a childnode? 1 Answer
Why are multiple objects being instantiated when I use clone, but not regular instantiation? 1 Answer
Cloning Problem Script 0 Answers
Instantiate random from a list gives error: "Reference not set to an instance" 1 Answer