Point System help?

I have a point system like Cod Zombies set up in my game, where you can purchase doors and it subtracts points from your point balance.
I also have a text element on screen that I want to display the current points. How do I constantly update the text so it always displays the points that I have?
For instance, here is the simple script that stores points as a variable:

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

public class PointManager : MonoBehaviour
{
    public int points;
    public Text ScoreText;

    void Awake ()
    {
        points = 1000;
        ScoreText.text = "" + points;
    }
}

do I add a “void Update()” section? How would I do this?

Updating the text for your points is nice and simple. It’s similar to how you’ve already done it in your Awake.

Option 1:
Usually the way I handle that stuff is just to update as needed. I’m not sure if there’s any issue with updating it all the time in an update statement. I like to keep the updates to an absolute minimum though, so I personally just do stuff like that when something changes.

Example, say you had this:

points += 10;

You would add this right after:

ScoreText.text = points.ToString();

Option 2:
Some might just simply do this, but again, aside from it being unnecessary to do it every frame, I’m not sure if there’s any downside to it performance wise (I would think it’s probably super minimal, unless you have a lot going on though… someone more wise with this can answer that though):

     void Update ()
     {
          ScoreText.text = points.ToString();
     }

Option 3:
I like the first way, myself, but it might be easy to forget to add it somewhere. Or, you could even create a function:

void SetPoints (int newValue)
{
     points = newValue;
     ScoreText.text = points.ToString();
}
.....
// Whenever you need to update your points.... call it like so, and it will always update the text field for you:
SetPoints (points + 10);
1 Like