Need to know what this is doing. (Static variables)

I am not familiar with static classes. With that being said I know that statics do provide a way to work around certain null reference issues and also provide a way to call methods directly from other classes without making them public. However I’m still ignorant to what this code is doing and how the static member variables are working. If anyone could clue me in I would greatly appreciate it!

using UnityEngine;
using UnityEngine.UI;

public class ScoreText : MonoBehaviour
{
    public static ScoreText _instance;
    public static ScoreText instance { get { return _instance; } }
    private int score;
    private Text scoreText;

    private void Awake()
    {
        scoreText = FindObjectOfType<Text>();
        if (_instance != null && _instance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            _instance = this;
        }
    }

    void Start()
    {
        score = 0;
        scoreText.text = score.ToString();
    }

    public void AddToScore(int points)
    {
        score += points;
        scoreText.text = score.ToString();
    }

    public void ResetScore()
    {
        score = 0;
        scoreText.text = score.ToString();
    }
}

If you want to know about this script only don’t read first paragraph just a general understanding of static variables in first paragraph. Read the second one.

I had trouble understanding the static variables at first. So I know what problem you are facing. I will just make it simple for you. Ok if you have like 100 enemies. So will you write 100 enemy scripts? Nope! You will write one and have something like hp over it and then use it with 100 gameobjects. Now suppose your player hits one enemy an his hp is now 90 but then I can argue that you used the same script on evry enemy so everyone’s hp should be 90. That thing was just an instance of the actual class so only one enemies hp is down. But if it were static you would notice that now the hp is a class function not an instance function so every enemy’s hp would be 90 in layman terms.

So in the above script we want that only one scoremanager toe exist in the game. Surely you don’t want that each and every gameObject calling has its own version of scoremanager. Just like enemies hp in the above case. If there were multiple scoremanagers then for one gameobject using it the score will be 00 and for other 10. We don’t want this behaviour we want each gameobject to look at one scoremanager and then modify the information in it. And static help us do that one instance in the whole game. The above pattern is called singleton pattern. And therefore anytime you add score you need not to write

Scoremanager scoreManager;
void Start()
{
  scoremanager = GameObject.Find("Score Manager").GetComponent<ScoreManager>();
}

when you write this you are grabbing an instance of it instead you write but as it is only one and a class property not an object you write. scoremanager.instance.something…

1 Like

Thank you so much for relating to me and helping me understand!

No problem always here to help.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms