0

I'm making an course on udemy. The lesson explain how to trigger a function that stops the world movement when the player hits a hazard. I copied it exactly as the class shows.

This is the "PlayerController" script which has the trigger to call the function that stops the world movement.

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

public class PlayerController : MonoBehaviour

{
    public GameManager theGM;
    
    // Start is called before the first frame update
    void Start()
    {
    
    }

    // Update is called once per frame
    void Update()
    {
    
    }

    
    public void OnTriggerEnter(Collider other) {
        if (other.tag == "Hazards") {
            Debug.Log("Hit Hazard");
            theGM.HitHazard();
        }
    }
}

And this is the "GameManager" script which contains the function that stops the world movement.

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

public class GameManager : MonoBehaviour
{
    public bool canMove;
    static public bool _canMove;
    public float worldSpeed;
    static public float _worldSpeed;    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        _canMove = canMove;
        _worldSpeed = worldSpeed;   
    }

    public void HitHazard() {
        _canMove = false;
        canMove = false;
    }
}

When the player collides with a hazard its runs the "OnTriggerEnter" function, it shows on the console that the player hit a hazard, but when it tries to call the "HitHazard" function the following error appears: "NullReferenceException: Object reference not set to an instance of an object".

Already tried to rewrite both codes and search in the internet, but still could not solve it.

Jason
  • 2,493
  • 2
  • 27
  • 27
  • 1
    NullRefernceException appear when you forgot to set your field in the inspector. You should probably check that. – IndieGameDev Aug 05 '20 at 19:47
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – BugFinder Aug 05 '20 at 21:24

3 Answers3

0

A NullReferenceException is when you try to access something (field/property/method) on a null instance.

object obj = null;
obj.Something(); // <<< NullReferenceException is throwed

In unity you can get more informations there (File name & line number): 1

Edit: In your code the only thing that can be null is your GameManager reference "theGM" in your PlayerController. Try to bind it correctly in the editor, or use the Singleton pattern.

I hope i helped you

Orkad
  • 630
  • 5
  • 15
0

NullReferenceException means that a field you are trying to access is null, Unity logs lines where does it happen, usually appearing logged as FunctionPath (at file: xx)

xx - Line where it occured.

file = Path to the file in your Assets folder.

You can fix it in many ways, here are two.

  1. Make Awake function with code that adds the GameManager Component to active GameObject:
void Awake()
{
   theGM = base.AddComponent<GameManager>();
}
  1. Add the GameManager Component to GameObject in Unity and assign it to the field theGM
0

Thank you guys. The problem was that I didn't set the GameManager in the inspector. I tried adding it to the object field in te inspector but still the error continued, I think because I changed something in the code while trying to fix it. So I rewrited te code again and added the GameManager object to the Inspector, now it works.

public class PlayerController : MonoBehaviour

{
    public GameManager theGM;
    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    
    public void OnTriggerEnter(Collider other) {
        if (other.tag == "Hazards") {
            Debug.Log("Hit Hazard");
            theGM.HitHazard();
        }
    }
}