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.