Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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++;
    }
    }