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();
}
}