how to make a Fail counter (raise a certain value depending on a certain action)

i am VERY new to Unity and coding and i’m making my first game in which i’m trying to raise text value by 1 every time the player collides with an obstacle and the scene restarts. after many many hours i’m left with this code:

using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{
    public Collision collision;
    public Transform player;
    public Text score;
    public int start = 0;
    public int failCount;

    private void Start()
    {
        failCount = start;
        score.text = failCount.ToString();
    }
    private void OnCollisionEnter(UnityEngine.Collision collisioninfo)
    {
        if (collisioninfo.gameObject.tag == "Obstacle")
        {
            failCount += 1;
        }
    }
}

no errors but the code simply does nothing, but I’m also very stubborn and even though i will never do anything with this terrible game I still want to achieve what I have desired and I really wanted to figure it out on my own but i just can’t.

What did you expect the code to do? Unity only ever does what you tell it to so, and you told it to

  • set up failcount
  • set the score text to a string representation of failcount

And whenever you collide, you tell unity to

  • increase fail count

If you expect the text to update after a collision, you will have to add code for that. Currently, it’s just counting, and not updating the text.

i know, i mentioned i’m very new to coding and that’s exactly what my problem is, i don’t know how to set up a code that will update the text

Ah, but you already have the code :slight_smile:

score.text = failCount.ToString();

add it directly after you update the counter (i.e. the very next line), and you are all set.

(that being said, I’m quite sure itshould be score-text