Yeild and WaitForSeconds and Instantiate

I am having trouble with waitforseconds and instantiating my player.

function Dead()
{
	Destroy(gameObject);
	Instantiate(deadPlayer, transform.position, transform.rotation);
	
	yield WaitForSeconds(2);
		Instantiate(Player, spawnPoint.transform.position, transform.rotation);
		enabled = false;
}

Yield seems to be working perfectly in my enemy script where it is supposed to destroy itself.

Thanks!

1 Answer

1

its simple, You destroy itself , if that script is attached to your Character,it will Dsetroy The whole Object, So, before yield work, You destroy the Whole Script.
However, you can make a “Spawn script” with a simple State but make sure you write the Script to a new Script. and make a Parent for your character, then Attach your new Spawn Script to your Parent Object,
so Maybe something like this :

Var MyChild : GameObject;
function Dead()
{
  Instantiate(deadPlayer, transform.position,   transform.rotation);    
  Destroy(MyChild);
  
 
   yield WaitForSeconds(2);
   Instantiate(Player, spawnPoint.transform.position, transform.rotation);
   enabled = false;
}