I am making game but i want the SceneManager to reload Scene if i die but after all the things i added happen first. this is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour
{
// public
public AudioSource audioSource;
public SoundManager sm;
public GameObject Player;
public ParticleSystem ps;
public int health;
public int numOfHearts;
public Image[] hearts;
public Sprite fullHeart;
public Sprite emptyHeart;
// void
void Update()
{
if (numOfHearts <= 0)
{
Instantiate(ps, Player.transform.position, Player.transform.rotation);
audioSource.Stop();
if (sm) {
sm.DeathS();
} else {
Debug.Log("Did not find the Sound Manager");
}
Destroy(Player);
PlayerDies();
}
if (health < numOfHearts)
{
health = numOfHearts;
}
for (int i = 0; i < hearts.Length; i++)
{
if (i < health)
{
hearts[i].sprite = fullHeart;
}else
{
hearts[i].sprite = emptyHeart;
}
if (i < numOfHearts)
{
hearts[i].enabled = true;
}else
{
hearts[i].enabled = false;
}
}
}
IEnumerator PlayerDies()
{
if (Player == null)
{
yield return new WaitForSeconds(1);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
}