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

Question Object reference not set to an instance of an object (script name).OnTriggerEnter

Discussion in 'Scripting' started by heemebeeme, Jun 12, 2022.

  1. heemebeeme

    heemebeeme

    Joined:
    Jun 9, 2022
    Posts:
    3
    I've been trying to figure out a way to fix the error Object reference not set to an instance of an object (script name).OnTriggerEnter. It does give me the line its on (14) but I am fairly new to scripting and was using a tutorial and got the error I have no idea how to fix it. Can someone help me out?


    Script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class ScoringSystem : MonoBehaviour
    {
    public GameObject ScoreText;
    public int theScore;

    void OnTriggerEnter(Collider other)
    {
    theScore += 1;
    ScoreText.GetComponent<Text>().text = "Pages Collected: " + theScore;
    Destroy(gameObject);
    }
    }
     
  2. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Do you have a text attached to the ScoreText game object?
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    Kurt-Dekker likes this.
  4. heemebeeme

    heemebeeme

    Joined:
    Jun 9, 2022
    Posts:
    3
    Yes I do