What is wrong with this script. All this script was meant to do is when the trigger gets triggered then the player dies, then the player gets destroyed, then restart the current level, and finally spawn the player?

using UnityEngine;

using System.Collections;

public class ResetLevel : MonoBehaviour

{

private PlayerHealth playerHealth;
public Vector3 spawnPoint;
private bool playerIsDead;
public GameObject player;

void Awake()
{
	playerHealth = GetComponent<PlayerHealth>();
}

void Update()
{
	if(playerIsDead)
	{
		Invoke("LoadedLevel", 3);
	}
}
void OnTriggerEnter(Collider col)
{
	if(col.gameObject.tag == "Player")
	{
		playerHealth.currentHealth = 0;
		Destroy(player);
		playerIsDead = true;
		LoadedLevel();
	}
}
public void LoadedLevel()
{
	Application.LoadLevel(Application.loadedLevel);
	Spawn();
}
void Spawn()
{
	Instantiate(player, spawnPoint, Quaternion.identity);
}

}

Hi “Unity_Noob_Need_Help”, You have the right idea with this script. The only problem is in the method LoadedLevel(), you are calling Application.LoadLevel(Application.loadedLevel); That is restarting the entire scene and thus not running the following code.
There are two workarounds for this.

  1. Use DontDestroyOnLoad(gameObject); in the Awake function.

What this will do is keep this object present and NOT reset this specific object thus running the code following “Application.LoadLevel(Application.loadedLevel);”

The more preferable:

  1. Instead of having the Spawn() function in the LoadedLevel() function, put it in the Start() function that way it will spawn every time the scene loads.

I hope this helps, happy coding :slight_smile:

~nDrummR

seems issue is with Update function, you should never add

if(playerIsDead)
     {
         Invoke("LoadedLevel", 3);
     }

in Update function, never. remove it & scripts should work!