Search Unity

OnTriggerEnter not working

Discussion in 'Scripting' started by Nicoparadise, Feb 12, 2018.

  1. Nicoparadise

    Nicoparadise

    Joined:
    May 12, 2016
    Posts:
    7
    Here's my problem that i've been trying to solve for the past couple of days:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Coin : MonoBehaviour {
    7.  
    8.     private void OnTriggerEnter(Collider other)
    9.     {
    10.         if (other.tag == "Player")
    11.         {
    12.             Debug.Log("hello");
    13.             //ScoreManager.Instance.GetCoin();
    14.             Destroy(this.gameObject,1f);
    15.         }
    16.     }
    17. }
    Now, when the Player collides with the coin, it outputs hello. But, when i uncomment the commented line, it throws a NullReferenceException. Now, i do have a script that's named ScoreManager. I don't understand why this happens.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Show ScoreManager script.

    Are you setting the Instance variable = to "this" in Awake or Start in ScoreManager?

    NullReferenceExceptions are pretty easy to solve. It means something doesn't have a value, thus it is null, but you are trying to access it. So Instance is null.
     
    Nicoparadise likes this.
  3. Nicoparadise

    Nicoparadise

    Joined:
    May 12, 2016
    Posts:
    7
    All right! I Instanced it and now it works! Thank you very much.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Glad you got it working :)