Unable to re-spawn after colliding with enemy

Hi I am new to unity, I have written a re-spawn which works if the character falls off the edge of the map, however once the player collides with the enemy the re-spawn text will appear and the game will stop, however once I press enter nothing happens and the player is stuck. Any help would be great thanks :slight_smile:

Here is my code:

onTriggerEnter()
{
if(other.gameObject.tag == “enemy”)
{
deadText.text = “You died Press Enter to Continue”;
Respawn();
}

}

void Respawn()
{
Time.timeScale = 0.0f;

	if(Input.GetKey("enter")) 
	{
			Time.timeScale = 1.0f;
			deadText.text = "";
			transform.position = transform.position = new Vector3    (0f, 2f, 0f);
	}
}

That’s because if(Input.GetKey("enter"))... only happens once, inside your Respawn() function, which is only called once in onTriggerEnter(). That if statement only checks for the return button at the exact frame the trigger happens. After that, the game is “paused” and Respawn() is never called again.

To solve this, you need to remember that the player died. There are literally hundreds of ways to do this, depending on your game. The easiest way is to probably to make something like

private bool playerDead = false;

private void onTriggerEnter()
{
    if (other.gameObject.tag == "enemy")
        Die();
}

private void Update()
{
    if (playerDead)
        if (Input.GetKey("enter"))
            Respawn();
}

private void Die()
{
    Time.timeScale = 0.0f;
    deadText.text = "You died Press Enter to Continue";
    playerDead = true;
}

private void Respawn()
{
    Time.timeScale = 1.0f;
    deadText.text = "";
    transform.position = new Vector3(0f, 2f, 0f);
    playerDead = false;
}