In my game I would like to restart the scene once my player loses his health, so far he dies and the particle effects show but the scene doesn’t restart. Could someone show me what I am doing wrong, thank you!
You are destroying the very script that is supposed to restart the scene. gameObject is a reference to self.
_
public class DeleteOnStart : MonoBehaviour
{
void Start()
{
Destroy(gameObject);
}
}
_
The example above will destroy whatever gameobject that script is attached to on start. You need to decouple the actions of destroying the player and reloading the scene somehow. is your script attached to the player? You could try something like this in a script not attached to the player.
_
// reference the player gameobject in the script responsible for destroying the player and
// reloading the scene.
public GameObject playerGO;
// destroy the reference player gameobject and then start the coroutine
Destroy(playerGO);
StartCoroutine(ReloadScene());
_
public class GameManager: MonoBehaviour
{
public GameObject playerGO;
public void RestartScene()
{
Destroy(playerGO);
StartCoroutine(LoadScene());
}
IEnumerator LoadScene()
{
yield return new WaitForSeconds(1.5f);
SceneManager.LoadScene("1");
}
}
_
public class Player : MonoBehaviour
{
public GameObject GameManagerGO;
public int currentHealth;
void Update()
{
if (currentHealth <= 0)
{
GameManagerGO.GetComponent<GameManager>().RestartScene();
}
}
}