I'm creating a 3D endless runner in unity, and I have created an UI that displays the number of coins collected, but if I try to run the script I get this error : NullReferenceException: Object reference not set to an instance of an object CollectableControl.Update () (at Assets/Scripts/Collectables/CollectableControl.cs:18)
This is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CollectableControl : MonoBehaviour
{
public static int coinCount;
public GameObject coinCountDisplay;
/*void Start()
{
coinCountDisplay = GameObject.Find("CoinCount");
}*/
void Update()
{
coinCountDisplay.GetComponent<Text>().text = "" + coinCount;
}
I also placed the GameObject that needs to be accessed by the script where it should be
I tried to assign the coinCountDisplay field to the GameObject with the name "CoinCount" in void Start() but that didn't solve the issue, neither restarting the Unity Editor, deleting and recreating the script and adding the GameObject like in the attached picture.