- Home /
Create a new GameObject on a destroyed GameObject position
Hello, I'm new using unity and I would like to ask how to make a new GameObject on the exact position of another one which was destroyed. This is because when an enemy dies I want to make it change from a "standing" sprite to a "dead" one.
I guess I gotta do it here
public void Dead()
{
Destroy (gameObject);
}
But... How do I call a new gameObject at same position when this one is destroyed ?
Answer by Hellium · Jun 28, 2015 at 09:58 AM
It's not different from a simple instantiation :
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
See doc : http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
And yes, the Insantiate
call should be in your Dead function.
Be carefull, you must instantiate the gameobject before you destroy the first one. Also, make sure to not set the parent of your dead sprite to your object which will be destroyed. Otherwise, the dead sprite will be destroyed too.
GameObject deadSprite = (GameObject) GameObject.Instantiate( DeadSpritePrefab, transform.position, Quaternion.identity ) ;
Your answer
Follow this Question
Related Questions
Find gameobjects by tag, and destroy if in the distance of a certain game object... 1 Answer
How to destroy intiated gameobject prefab from controller script 2 Answers
Best way to include multiple game themes into the game each with different game objects? 1 Answer
destroy delete the component, not the game object 1 Answer
Destroy gameobject component that inherits from a certain class 3 Answers