Lives script

Hello,
In my game the player has 3 lives and when it collides with an object with the tag “avoid_me” it should lose 1 life, but instead it loses 2 lives per time, why does this happen? Here the codes (c#) :

void Start()
        {
            PlayerPrefs.SetInt("Lives", 3); //setting players initial lives.
        }
    }
 int Lives;
    void Start()
    {
        UpdateUI();

    }
    void UpdateUI()
    {

        Lives = PlayerPrefs.GetInt("Lives");

        GameObject.Find("livesUI").GetComponent<Text>().text = "Lives : " + Lives;
    }
 
    void OnCollisionEnter2D(Collision2D coll)

    {
        string tag = coll.collider.gameObject.tag;



        if (tag == "avoid_me")
        {
          
            Lives = PlayerPrefs.GetInt("Lives");
            Lives--;
            PlayerPrefs.SetInt("Lives", Lives);

         
            if (Lives > 0)
                SceneManager.LoadScene(SceneManager.GetActiveScene().name);

Any help would be really appreciated, ty.

Looks okay to me… You could try this after “Lives–”:

print("Life lost: " + Lives + "  collision obj: " + coll.gameObject.name + "   game obj: " + gameObject.name);

You can use this (little better, no allocation):

if(coll.gameObject.CompareTag("avoid_me"))

and also, you don’t have to get the Lives variable again on collision, because you already stored it in Start. :slight_smile:

1 Like

I just removed Lives = PlayerPrefs.GetInt("Lives"); from the code and it works! I lose 1 life at a time…I suppose it was decreasing by 2 because I stored it 2 times, and the Lives-- was decreasing two times? hehe

Well, GetInt was retrieving it. Hm. lol I’m glad it’s solved. I had just mentioned that you could remove that, not even thinking it would solve your problem. Had to be something else :slight_smile:

One more thing bro.
Now I am working on an object that increases the life of the player by 1 when the player collides with it (but it doesn’t feel like a “brick” because I use trigger). So the new script is :

  int Lives;
    void Start()
    {
        UpdateUI();

    }
    void UpdateUI()
    {

        Lives = PlayerPrefs.GetInt("Lives");

        GameObject.Find("livesUI").GetComponent<Text>().text = "Lives : " + Lives;
    }
    void OnTriggerEnter2D(Collider2D collider)
    {
     
        if (collider.gameObject.name == "Life")
        {
        Destroy(collider.gameObject);
        Lives++;
        PlayerPrefs.SetInt("Lives", Lives);
      }
    }


    void OnCollisionEnter2D(Collision2D coll)

    {
        string tag = coll.collider.gameObject.tag;



        if (tag == "avoid_me")
        {
    
           Lives--;
            PlayerPrefs.SetInt("Lives", Lives);

         
            if (Lives > 0)
                SceneManager.LoadScene(SceneManager.GetActiveScene().name);

In the case you touch an object with tag “avoid_me”, it all goes well, the player loses 1 life and the life text decreases by 1, but in the case a player collides with the “Life”, yes it gets a life, but why it doesn’t show?
For example, if I have 3 lives, I touch a “Life”, and then I go on an object with tag “avoid_me”, I have 3 lives. If I get 10 “Life”, and then I touch an object with “avoid_me”, I have 12 lives.
So it increases the lives but why it doesn’t display as when it loses lives? What’s wrong with the code?[/code]

Well, your posted code is incomplete. You must be setting the text somewhere when it’s updated (for losing a life).
You just need to add the same kind of text update when you gain a life.

One other thing – you could create a variable for the Text game object and connect it in the inspector. Use that variable to do your updates, rather than GameObject.Find, like you have written in UpdateUI.

:slight_smile: