- Home /
How to spawn diferent game objects in an array?
I have been trying for hours to get this to work and I have not come up with the answer searching either. What I am try to do in JScript: Spawn different replacements after my enemies are destroyed. For example when it is destroyed I want an explosion to instantiate in its place and and sometimes a wreakage as well. Here is the code I am using but nothing happens after an enemy is destroyed:
#pragma strict
var hp : float;
var replacementSpawn : GameObject[];
var replacement : GameObject;
//Take Damage Function
//Subtract damage from hp
function TakeDamage( damage : float )
{
hp = hp - damage;
if(hp <= 0)
{
var i : float = 0;
while( i < replacementSpawn.Length)
{
Instantiate (replacement, replacementSpawn[i].transform.position, replacementSpawn[i].transform.rotation);
i++;
}
Network.Destroy(gameObject);
}
}
Answer by Owen-Reynolds · Feb 19, 2013 at 03:35 AM
See the docs: http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html or search "Unity Instantiate." It's the second example. Shows how to spawn something and keep the link to it.
In your case, you'd need replacementSpawn[i]=
Thank you so much for the answer I was doing all these different things, when it was just right there. Can't believe it haha.
Your answer
Follow this Question
Related Questions
Game Object Chains 1 Answer
Finding the Sum of Values of Multiple GameObjects in an Array + Variable Sized arrays 0 Answers
Filling array with gameObjects 2 Answers
GameObject Array in Editor GUI 3 Answers