I am a relatively new C# coder. I have never had a problem with this before but for some reason when I try to update my score through code. it breaks my game. here is my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour
{
public static ScoreManager instance;
[SerializeField] Text score;
int points;
void Start()
{
if(instance == null)
{
instance = this;
}
score = GetComponent<Text>();
points = 0;
}
// Update is called once per frame
void Update()
{
score.text = points.ToString();
}
public void AddToScore()
{
points++;
}
}
If I comment out the score.text = points.ToString(); it will work fine. but it won't update.
Can someone help me this please :(. I am using unity, and I have attached the text file to the scoremanager object. The error i get is this:
NullReferenceException: Object reference not set to an instance of an object ScoreManager.Update () (at Assets/Scripts/ScoreManager.cs:27)