Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Player sstats and UI stop working

Discussion in 'Scripting' started by vivianindatub, May 8, 2021.

  1. vivianindatub

    vivianindatub

    Joined:
    Apr 23, 2021
    Posts:
    71
    Player stats and UI stop working once I changed back to previous scene? So I have player with health, hunger, and hygine bar that decrease automatically. I am planning to have the player enter different areas in my game (switching scenes). The first switch from area 1 to area 2 works fine with the health stats decreasing BUT THE UI BAR DISAPPEARS. However when I SWITCH BACK TO AREA 1 the stats just stop completely. I added a don't destroy on load on the scripts but still.
    Below is my code for the player stats:
    ublic class PlayerStats : MonoBehaviour
    {
    public float health = 100;
    public float hunger = 100;
    public float hygine = 100;
    public float deathRate;
    public float hungerRate;
    public float hygineRate;
    public Slider HealthBar;
    public Slider HungerBar;
    public Slider HygineBar;
    public static PlayerStats Instance;
    private void Start()
    {
    GameObject.DontDestroyOnLoad(this.gameObject);
    }
    // Update is called once per frame
    void Update()
    {
    GameObject.DontDestroyOnLoad(this.gameObject);
    /*stats bar*/
    HealthBar.value = health;
    HungerBar.value = hunger;
    HygineBar.value = hygine;
    hunger = hunger - (hungerRate * Time.deltaTime);
    hygine = hygine - (hygineRate * Time.deltaTime);
    if (health <= 0)
    {
    health = 0;
    Debug.Log("Dead");
    }
    if (hunger <= 0 || hygine <= 0)
    {
    health = health - (deathRate * Time.deltaTime);
    }
    if (hunger <= 0)
    {
    hunger = 0;
    }
    if (hygine <= 0)
    {
    hygine = 0;
    }
    //set max value to 100
    if (health >= 100)
    {
    health = 100;
    }
    if (hunger >= 100)
    {
    hunger = 100;
    }
    if (hygine >= 100)
    {
    hygine = 100;
    }
    }
    }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    You have DontDestroyOnLoad but no way to make sure that there is not another instance of the object in the scene.

    When you load back to area 1, you'll have a second copy of this script in the scene, both of them in DontDestroyOnLoad.
     
  3. vivianindatub

    vivianindatub

    Joined:
    Apr 23, 2021
    Posts:
    71
    I know I'm supposed to figure out a way to delete duplicate but there's not even duplicates?? When I go back to scene 1 how come there's nothing at all?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    What do you mean there's nothing at all? Look at your hierarchy after loading the scenes. You will see this object under the DontDestroyOnLoad section.
     
  5. vivianindatub

    vivianindatub

    Joined:
    Apr 23, 2021
    Posts:
    71
    yes but the UI panel disappeared on screen