Это может быть, если scoreGT равно null.
Вы обращаетесь к его полю text, и возникает ошибка.
Ведь поля имеются только у существующих объектов, а у вас вместо объекта null.
Очевидно, проблема строчкой выше:
scoreGT = scoreGO.GetComponent();
Вот здесь задается scoreGT = null.
Это говорит о том, что на scoreGO (который ScoreCounter) не висит компонент Text.
У него может быть дочерний объект типа Text, но это не то: мы ищем компонент, то есть секцию Text, что справа в инспекторе редактора Unity.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Basket : MonoBehaviour
{
[Header("Set Dynamically")]
public Text scoreGT;
private void Start()
{
GameObject scoreGO = GameObject.Find("ScoreCounter");
scoreGT = scoreGO.GetComponent<Text>();
scoreGT.text = "0";
}
private void Update()
{
Vector3 mousePos2D = Input.mousePosition;
mousePos2D.z = -Camera.main.transform.position.z;
Vector3 mousePos3D = Camera.main.ScreenToWorldPoint(mousePos2D);
Vector3 pos = this.transform.position;
pos.x = mousePos3D.x;
this.transform.position = pos;
}
private void OnCollisionEnter(Collision coll)
{
GameObject colldedWith = coll.gameObject;
if (colldedWith.tag == "Apple")
{
Destroy(colldedWith);
int score = int.Parse(scoreGT.text);
score += 100;
scoreGT.text = score.ToString();
}
}
}
Ошибка в этой строчке: scoreGT.text = "0";