Score counter

hi, I’ve made this counter where score should increase if you catch heart and decrease when you run into fire, but it;s only showing me 100/100 score. That are the initial values I put for total life and life in the game. Can you help me, what is my mistake?

using System.Collections;
using System.Collections.Generic;

using System.Globalization;
using System.Net.NetworkInformation;
using UnityEngine;
using UnityEngine.UI;

public class destroygameobject : MonoBehaviour
{
    private string lifeactually = "100";
    private string lifetotal = "100";
    private int lifeingame = 100;
    public Text textforlife;
    public GameObject particlesystem;



    void Update()
    {
        textforlife.text = "Life " + lifeactually + "/" + lifetotal;

    }
    void OnCollisionEnter2D(Collision2D col)
    {
     
        if (col.gameObject.tag == "Heart")
        {
            // destroy this object
            Destroy(col.gameObject);
            Debug.Log("Object Destroyed");
            particlesystem.SetActive(true);
            if (lifeingame < 100)
        
            {
                lifeingame += 10;
                lifeactually = lifeingame.ToString();
             
            }
            textforlife.GetComponent<UnityEngine.UI.Text>().text = "Life " + lifeactually + "/" + lifetotal;
         
        }
        else if (col.gameObject.tag == "Fire" && lifeingame > 0)
        {
            lifeingame -= 10;
            lifeactually = lifeingame.ToString();
            textforlife.GetComponent<UnityEngine.UI.Text>().text = "Life " + lifeactually +"/" + lifetotal;

        }
        else
        {
            Debug.Log("You died");
        }

    }
}

Nope, please edit your post according to this: https://discussions.unity.com/t/481379

Looks like you’re already familiar with Debug.Log()… time to sprinkle a lot more of them throughout your code that displays the values of critical numbers at each point in the code, to see what is actually happening.

I do NOT recommend storing things in a string like that. Keep the score as an integer, then only turn it into a string when you move it to the UI.Text object’s .text field.

Use gameObject.ComprareTag instead and make sure that the tag actually is the same on the other gameobject and actually these two are colliding.
Same applies to the “Fire” tag as well.

1 Like