Search Unity

How to Build a Death counter with Player Prefs

Discussion in 'Scripting' started by K_O_N_S_T_Y, Sep 13, 2019.

  1. K_O_N_S_T_Y

    K_O_N_S_T_Y

    Joined:
    Feb 24, 2019
    Posts:
    45
    Hi i would like to make a Death counter with PlayerPrefs, my Problem is the Player die if he fall from a Platform
    and his Position get under -12. if i write:

    if (transform.position.y < -12)
    {

    counter++;
    Time.timeScale = 0f;
    hudEnd.SetActive(true);



    }

    is updating for every Frame and the value get a Higher value.
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    355
    So when your character falls below -12 he dies, and you want counter++ to only be called once, not every frame? If so, simply introduce a bool "isDead", which you set true when the character is dead, and on which you can base further decisions. Example:
    Code (CSharp):
    1. if (transform.position.y < -12 && !isDead)
    2. {
    3.     isDead = true;
    4.     counter++;
    5.     Time.timeScale = 0f;
    6.     hudEnd.SetActive(true);
    7. }
    Remember to reset the position and isDead (to false) when the player respawns. Doing this makes it so that counter is incremented once for each time the player dies.
    Also, please use code tags in the future like above. Makes reading examples easier.
     
  3. K_O_N_S_T_Y

    K_O_N_S_T_Y

    Joined:
    Feb 24, 2019
    Posts:
    45
    thanks for help but i found a another way which is even similar to your solution
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,171