Search Unity

Question what is the problem?

Discussion in 'UGUI & TextMesh Pro' started by bugon99, Oct 10, 2023.

  1. bugon99

    bugon99

    Joined:
    Oct 10, 2023
    Posts:
    1
    I want to score up if bird go through tunnel.
    But I have this error. and no score-up.

    NullReferenceException: Object reference not set to an instance of an object
    Score.Update () (at Assets/Score.cs:20)

    score.cs

    using System.Collections;
    using System.Collections.Generic;
    using TMPro;
    using UnityEngine;
    using UnityEngine.UI;
    public class Score : MonoBehaviour
    {
    public static int score = 0;
    // Start is called before the first frame update
    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {
    GetComponent<Text>().text = score.ToString(); // str(score) < -- 20 line
    }
    }

    Scoreup.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class scoreup : MonoBehaviour
    {
    private void OnTriggerEnter2D(Collider2D collision)
    {
    Score.score++;
    }
    }