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!