Counter issues?

Hello,
Right now I have a counter that is counting how many seconds i’m alive.
That’s all good however, when my health goes to 0 i switch scenes.
I would like to have my finalcount or seconds alive displayed.
However, i just can’t get ti to work…

What do I need to do?
Thanks

Current code:

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

public class PlayerHealth : MonoBehaviour {
   
    public float Health = 600;
    public int healthRate = 5;
    public float timer = 0F;
    public bool playerDead = false;
    public double counter = 0;
    public int iCounter;
    public int FinalCount;
    public Text t;
    public Text t2;
   
    public void damagePlayer (int damage){
        Health -= damage;
        if (Health <= 0)
        {
            Dead();
        }
    }
   
    private void Dead(){
        Destroy (this.gameObject);
        playerDead = true;
        Application.LoadLevel("RespawnMenu");
    }
    private void Update ()
    {
        timer += Time.deltaTime;
        counter += Time.deltaTime;
        iCounter = (int)counter;
        t.text = "" + iCounter;
        FianlCounter ();
        t2.text = "" + FinalCount;

        if (timer > 2) {
            Health += healthRate;
            timer = 0;
        }
        if (Health > 600) {
            Health = 600;
        }
    }
        private void FianlCounter(){
            if (playerDead) {
                FinalCount = iCounter;
            }
     }
}

@StarManta Help on this one too would make my day as this project is due tomorrow :slight_smile:

@StarManta I think part of the problem is that t and t2 are in different scenes but I don’t know how to get around this

This code seems like it could be a lot simpler. The problem might be that you’re destroying the object (line 27) and it stops executing the Update. Hence it never reaches FianlCounter () when playerDead is finally true.

Don’t FianlCounter() in Update() because you know exactly when it should happen - when the player is dead.
Try putting this code:

  • FianlCounter ();
  • t2.text = “” + FinalCount;

As the first two lines of the Dead() method and it might work. But generally think about whether you really need all that stuff in Update(). Make it cleaner

Yes, but the 2 texts are in different scenes? Does this matter?

Yup i reverted it to this

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

public class PlayerHealth : MonoBehaviour {
   
    public float Health = 600;
    public int healthRate = 5;
    public float timer = 0F;
    public bool playerDead = false;
    public double counter = 0;
    public int iCounter;
    public Text t;

   
    public void damagePlayer (int damage){
        Health -= damage;
        if (Health <= 0)
        {
            Dead();
        }
    }
   
    private void Dead(){
        Destroy (this.gameObject);
        playerDead = true;
        Application.LoadLevel("RespawnMenu");
    }
    private void Update ()
    {
        timer += Time.deltaTime;
        counter += Time.deltaTime;
        iCounter = (int)counter;
        t.text = "" + iCounter;
        if (timer > 2) {
            Health += healthRate;
            timer = 0;
        }
        if (Health > 600) {
            Health = 600;
        }
    }
       
     }

Now i need it to display on a different scene (RespawnMenu) the final time?

Bump…

http://stackoverflow.com/questions/22862932/keeping-scores-in-unity-and-pass-to-next-scene