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 ?

It’s not different from a simple instantiation :

public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);

See doc : Unity - Scripting API: Object.Instantiate

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 ) ;