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. Dismiss Notice

I am facing this problem how can i solve it?

Discussion in 'Getting Started' started by liewcarol50, Aug 22, 2023.

  1. liewcarol50

    liewcarol50

    Joined:
    Aug 8, 2023
    Posts:
    2
    upload_2023-8-22_11-8-59.png

    NullReferenceException: Object reference not set to an instance of an object
    GemSilver.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/GemSilver.cs:13) //This is the error


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GemSilver : MonoBehaviour
    7. {
    8.     public GameObject ScoreBox; // Assign the scoreBox GameObject in the Inspector.
    9.     public AudioSource collectSound;
    10.  
    11.     private void OnTriggerEnter(Collider other)
    12.     {
    13.         ScoreBox.GetComponent<Text>().text="1000";
    14.         collectSound.Play();
    15.         Destroy(gameObject);
    16.     }
    17. }
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    431
    1. Class GemSilver;
    2. Line 13;
    3. You want to call ScoreBox and an error shows up;
    4. You didn't drag and drop ScoreBox in Inspector to the GemSilver component;
    Such an error shows up always when you want to call something, if something doesn't exist it's null.
     
  3. liewcarol50

    liewcarol50

    Joined:
    Aug 8, 2023
    Posts:
    2
    upload_2023-8-22_21-28-32.png
    I already put it but still cannot work how to solve this problem because the yt video i study is long time ago that was not same version when i do this project