[SOLVED] Restarting upon death

I have a problem.
I’ve tried 5 different tutorials and scripts, but none work.
I want to restart the scene upon player death, restart the position and level (HP, DMG and DEF depend on the level).

Here is my “PlayerStats” script:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Audio;

public class PlayerStats : MonoBehaviour {

    public int currentLevel;
    public int currentEXP;
    public int [] toLevelUp;
    public int [] hpLevels;
    public int [] attackLevels;
    public int [] defenseLevels;
    public int currentHP;
    public int currentATK;
    public int currentDEF;
    public PlayerHealth pHealth;
    public Text levelUpText;
    public GameObject lvlUptext;
    public float timeToWait;
    public bool toShowLvlText;
    public AudioSource lvlUpSound;

    void Start ()
    {
        pHealth = FindObjectOfType<PlayerHealth> ();
        currentHP = hpLevels[1];
        currentATK = attackLevels[1];
        currentDEF = defenseLevels[1];
        levelUpText.enabled = false;
        lvlUptext.SetActive (false);
        toShowLvlText = false;
    }

    void Update ()
    {
        if(currentEXP >= toLevelUp[currentLevel])
        {
            LevelUp ();
            timeToWait = 2;
        }

        if(toShowLvlText = true)
        {
            timeToWait -= Time.deltaTime;
        }

        if(timeToWait <= 0)
        {
            levelUpText.enabled = false;
            lvlUptext.SetActive (false);
        }

        if(currentEXP < toLevelUp[currentLevel])
        {
            toShowLvlText = false;
        }
    }

    public void AddEXP(int expToAdd)
    {
        currentEXP += expToAdd;
    }

    public void LevelUp()
    {
        currentLevel++;
        currentHP = hpLevels[currentLevel];
        pHealth.playerMaxHP = currentHP;
        pHealth.playerCurrentHP += currentHP - hpLevels [currentLevel - 1];
        currentATK = attackLevels[currentLevel];
        currentDEF = defenseLevels[currentLevel];
        lvlUpSound.Play ();
        levelUpText.enabled = true;
        lvlUptext.SetActive (true);
        toShowLvlText = true;
    }
}

And here is my “UIManager” script that sees if the player’s health is below 0, and restarts the scene and player level (HP, DMG and DEF depend on the level).

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class UIManager : MonoBehaviour {

    public Slider hpBar;
    public Text hpText;
    public PlayerHealth playerHealth;
    private static bool UIExisting;
    public Canvas hpCanvas;
    private PlayerStats thePlayerStats;
    public Text levelText;
    public GameObject thePlayer;
    private PlayerHealth phealth;

    public void Start ()
    {
        if(!UIExisting)
        {
            UIExisting = true;
            DontDestroyOnLoad (transform.gameObject);
        }
        else
        {
            Destroy (gameObject);
        }
        thePlayerStats = GetComponent<PlayerStats> ();
    }

    public void Update ()
    {
        hpBar.maxValue = playerHealth.playerMaxHP;
        hpBar.value = playerHealth.playerCurrentHP;
        hpText.text = "HP: " + playerHealth.playerCurrentHP + "/" + playerHealth.playerMaxHP;
        levelText.text = "Lvl: " + thePlayerStats.currentLevel;

        if(playerHealth.playerCurrentHP <= 0)
        {
        }
    }
}

I’ve tried adding a timer when dying then restarting, but that just starts and restarts the scene every 3 seconds if you die.

Thanks in advance!

You already have it. Just put SceneManager.LoadScene("SceneName"); in the part you’re checking playerCurrentHP. You will need “using UnityEngine.SceneManagement;” at the top.

Or you mean to want to save/load the player’s level?

1 Like

You were almost right. I’ve solved the problem.

I have solved the problem: Here is the new code:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class UIManager : MonoBehaviour {

    public Slider hpBar;
    public Text hpText;
    public PlayerHealth playerHealth;
    private static bool UIExisting;
    public Canvas hpCanvas;
    private PlayerStats thePlayerStats;
    public Text levelText;
    public GameObject thePlayer;
    private PlayerHealth phealth;
    private float waitForRestartOnDeath;
    private PlayerController pController;
    public GameObject basicSword;
    public Text youDied;
    public Text restartingInText;

    public void Start ()
    {
        waitForRestartOnDeath = 3f;
        pController = FindObjectOfType<PlayerController> ();
        basicSword.transform.position = basicSword.transform.position;
        basicSword.transform.rotation = basicSword.transform.rotation;
        youDied.enabled = false;
        restartingInText.enabled = false;
        basicSword.transform.rotation = new Quaternion (transform.rotation.x, transform.rotation.y, transform.rotation.z, transform.rotation.w);

        if(!UIExisting)
        {
            UIExisting = true;
            DontDestroyOnLoad (transform.gameObject);
        }
        else
        {
            Destroy (gameObject);
        }
        thePlayerStats = GetComponent<PlayerStats> ();
    }

    public void Update ()
    {
        hpBar.maxValue = playerHealth.playerMaxHP;
        hpBar.value = playerHealth.playerCurrentHP;
        hpText.text = "HP: " + playerHealth.playerCurrentHP + "/" + playerHealth.playerMaxHP;
        levelText.text = "Lvl: " + thePlayerStats.currentLevel;

        if(playerHealth.playerCurrentHP <= 0)
        {
            waitForRestartOnDeath -= Time.deltaTime;
            youDied.enabled = true;
            restartingInText.enabled = true;
            restartingInText.text = "Restarting in: " + waitForRestartOnDeath;
            if(waitForRestartOnDeath <= 0)
            {
                playerHealth.playerCurrentHP = playerHealth.playerMaxHP;
                thePlayer.SetActive (true);
                pController.canMove = true;
                youDied.enabled = false;
                restartingInText.enabled = false;
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
                basicSword.transform.position = basicSword.transform.position;
                basicSword.transform.rotation = basicSword.transform.rotation;
            }
        }
    }
}

I’ve added

    basicSword.transform.position = basicSword.transform.position;
        basicSword.transform.rotation = basicSword.transform.rotation;

because of a bug where the sword’s rotation was reversed when restarting.
All I had to do in the inspector was attach the “You died” Text I created in the canvas to the script component, and also the “Restarting in” Text I’ve also made in the canvas and added it into the script component.