Search Unity

[SOLVED] Restarting upon death

Discussion in 'Scripting' started by Nikola310, May 19, 2016.

  1. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.Audio;
    5.  
    6. public class PlayerStats : MonoBehaviour {
    7.  
    8.     public int currentLevel;
    9.     public int currentEXP;
    10.     public int [] toLevelUp;
    11.     public int [] hpLevels;
    12.     public int [] attackLevels;
    13.     public int [] defenseLevels;
    14.     public int currentHP;
    15.     public int currentATK;
    16.     public int currentDEF;
    17.     public PlayerHealth pHealth;
    18.     public Text levelUpText;
    19.     public GameObject lvlUptext;
    20.     public float timeToWait;
    21.     public bool toShowLvlText;
    22.     public AudioSource lvlUpSound;
    23.  
    24.     void Start ()
    25.     {
    26.         pHealth = FindObjectOfType<PlayerHealth> ();
    27.         currentHP = hpLevels[1];
    28.         currentATK = attackLevels[1];
    29.         currentDEF = defenseLevels[1];
    30.         levelUpText.enabled = false;
    31.         lvlUptext.SetActive (false);
    32.         toShowLvlText = false;
    33.     }
    34.  
    35.     void Update ()
    36.     {
    37.         if(currentEXP >= toLevelUp[currentLevel])
    38.         {
    39.             LevelUp ();
    40.             timeToWait = 2;
    41.         }
    42.  
    43.         if(toShowLvlText = true)
    44.         {
    45.             timeToWait -= Time.deltaTime;
    46.         }
    47.  
    48.         if(timeToWait <= 0)
    49.         {
    50.             levelUpText.enabled = false;
    51.             lvlUptext.SetActive (false);
    52.         }
    53.  
    54.         if(currentEXP < toLevelUp[currentLevel])
    55.         {
    56.             toShowLvlText = false;
    57.         }
    58.     }
    59.  
    60.     public void AddEXP(int expToAdd)
    61.     {
    62.         currentEXP += expToAdd;
    63.     }
    64.  
    65.     public void LevelUp()
    66.     {
    67.         currentLevel++;
    68.         currentHP = hpLevels[currentLevel];
    69.         pHealth.playerMaxHP = currentHP;
    70.         pHealth.playerCurrentHP += currentHP - hpLevels [currentLevel - 1];
    71.         currentATK = attackLevels[currentLevel];
    72.         currentDEF = defenseLevels[currentLevel];
    73.         lvlUpSound.Play ();
    74.         levelUpText.enabled = true;
    75.         lvlUptext.SetActive (true);
    76.         toShowLvlText = true;
    77.     }
    78. }
    79.  
    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).

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class UIManager : MonoBehaviour {
    7.  
    8.     public Slider hpBar;
    9.     public Text hpText;
    10.     public PlayerHealth playerHealth;
    11.     private static bool UIExisting;
    12.     public Canvas hpCanvas;
    13.     private PlayerStats thePlayerStats;
    14.     public Text levelText;
    15.     public GameObject thePlayer;
    16.     private PlayerHealth phealth;
    17.  
    18.     public void Start ()
    19.     {
    20.         if(!UIExisting)
    21.         {
    22.             UIExisting = true;
    23.             DontDestroyOnLoad (transform.gameObject);
    24.         }
    25.         else
    26.         {
    27.             Destroy (gameObject);
    28.         }
    29.         thePlayerStats = GetComponent<PlayerStats> ();
    30.     }
    31.  
    32.     public void Update ()
    33.     {
    34.         hpBar.maxValue = playerHealth.playerMaxHP;
    35.         hpBar.value = playerHealth.playerCurrentHP;
    36.         hpText.text = "HP: " + playerHealth.playerCurrentHP + "/" + playerHealth.playerMaxHP;
    37.         levelText.text = "Lvl: " + thePlayerStats.currentLevel;
    38.  
    39.         if(playerHealth.playerCurrentHP <= 0)
    40.         {
    41.         }
    42.     }
    43. }
    44.  
    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!
     
  2. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,413
    You already have it. Just put
    Code (csharp):
    1. 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?
     
    Nikola310 likes this.
  3. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    You were almost right. I've solved the problem.
     
  4. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    I have solved the problem: Here is the new code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class UIManager : MonoBehaviour {
    7.  
    8.     public Slider hpBar;
    9.     public Text hpText;
    10.     public PlayerHealth playerHealth;
    11.     private static bool UIExisting;
    12.     public Canvas hpCanvas;
    13.     private PlayerStats thePlayerStats;
    14.     public Text levelText;
    15.     public GameObject thePlayer;
    16.     private PlayerHealth phealth;
    17.     private float waitForRestartOnDeath;
    18.     private PlayerController pController;
    19.     public GameObject basicSword;
    20.     public Text youDied;
    21.     public Text restartingInText;
    22.  
    23.     public void Start ()
    24.     {
    25.         waitForRestartOnDeath = 3f;
    26.         pController = FindObjectOfType<PlayerController> ();
    27.         basicSword.transform.position = basicSword.transform.position;
    28.         basicSword.transform.rotation = basicSword.transform.rotation;
    29.         youDied.enabled = false;
    30.         restartingInText.enabled = false;
    31.         basicSword.transform.rotation = new Quaternion (transform.rotation.x, transform.rotation.y, transform.rotation.z, transform.rotation.w);
    32.  
    33.         if(!UIExisting)
    34.         {
    35.             UIExisting = true;
    36.             DontDestroyOnLoad (transform.gameObject);
    37.         }
    38.         else
    39.         {
    40.             Destroy (gameObject);
    41.         }
    42.         thePlayerStats = GetComponent<PlayerStats> ();
    43.     }
    44.  
    45.     public void Update ()
    46.     {
    47.         hpBar.maxValue = playerHealth.playerMaxHP;
    48.         hpBar.value = playerHealth.playerCurrentHP;
    49.         hpText.text = "HP: " + playerHealth.playerCurrentHP + "/" + playerHealth.playerMaxHP;
    50.         levelText.text = "Lvl: " + thePlayerStats.currentLevel;
    51.  
    52.         if(playerHealth.playerCurrentHP <= 0)
    53.         {
    54.             waitForRestartOnDeath -= Time.deltaTime;
    55.             youDied.enabled = true;
    56.             restartingInText.enabled = true;
    57.             restartingInText.text = "Restarting in: " + waitForRestartOnDeath;
    58.             if(waitForRestartOnDeath <= 0)
    59.             {
    60.                 playerHealth.playerCurrentHP = playerHealth.playerMaxHP;
    61.                 thePlayer.SetActive (true);
    62.                 pController.canMove = true;
    63.                 youDied.enabled = false;
    64.                 restartingInText.enabled = false;
    65.                 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    66.                 basicSword.transform.position = basicSword.transform.position;
    67.                 basicSword.transform.rotation = basicSword.transform.rotation;
    68.             }
    69.         }
    70.     }
    71. }
    72.  
    I've added
    Code (CSharp):
    1.     basicSword.transform.position = basicSword.transform.position;
    2.         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.
     
unityunity