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

Object reference not set to an instance (OnTriggerEnter)

Discussion in 'Scripting' started by ItVang, Aug 12, 2022.

  1. ItVang

    ItVang

    Joined:
    Apr 3, 2022
    Posts:
    2
    Hello! I'm pretty new to C# so I decided to create a little pachinko-type game but now I get stuck when the ball enters the box collider and gives an error in the console.

    NullReferenceException: Object reference not set to an instance of an object
    CupScore.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/CupScore.cs:34)


    Cup scoring script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class CupScore : MonoBehaviour
    7. {
    8.     public GameObject ball;
    9.     GameManager gameManager;
    10.     public int score;
    11.     public TMP_Text pointValue;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         score = Random.Range(1, 100);
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         UpdateTextboxes();
    22.     }
    23.  
    24.     void UpdateTextboxes()
    25.     {
    26.         pointValue.text = score.ToString();
    27.     }
    28.  
    29.     private void OnTriggerEnter(Collider other)
    30.     {
    31.            gameManager.updateScore(score);
    32.     }
    33. }
    34.  

    Game Manager:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class GameManager : MonoBehaviour
    8. {
    9.     CupScore cupScore;
    10.     public TMP_Text scoreText;
    11.     private int totalScore;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.        
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.  
    22.     }
    23.  
    24.     public void updateScore(int score)
    25.     {
    26.        
    27.         scoreText.text = "Score: " + totalScore;
    28.         totalScore += score;
    29.     }
    30. }
    31.  
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    It tells you the error is on line 34 but your code doesn't have a line 34. You should always make sure you post the correct/full script.

    Anyway, this is the most common thing that beginners get caught by with C# on these forums so we have a pinned posted here: https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    In your case, it seems it's referring to line 32 above so "gameManager" is null which is expected because you don't set it anywhere. It cannot just automagically know a reference to an instance of the GameManager; you need to set it.
     
  3. ItVang

    ItVang

    Joined:
    Apr 3, 2022
    Posts:
    2
    Thanks for replying, I figured it out. Also, I'm pretty sure the reason why it said the error was declared on line 34 was that I cleared some code after the error was present so that's my bad.