Hi… I need your help
I have a script “GameControl” and a script “SecurityCamera”.
SecurityCamera checks if the player is seen by the camera and calls alarm. Alarm should be a game ending event and restart the level.
In GameControl I want to store variables and functions used by multiple things.
GameControl Script:
public class GameControl : MonoBehaviour {
public static GameControl control;
public void Respawn() {Code Stuff}
}
SecurityCamer Script:
void OnTriggerEnter(Collider other)
{
if (!other.CompareTag("Player"))
return;
GameControl.control.Respawn();
}
Now I have the problem that this gives me the error:
NullReferenceException: Object
reference not set to an instance of an
object EnemyCamera.OnTriggerEnter
(UnityEngine.Collider other) (at
Assets/EnemyCamera.cs:47)
(double clicking it marks “GameControl.control.Respawn();” in my IDE)
---------------------------------------------------------------------------
I looked it up in my old files, where I once did the same and it worked.
Exactly the same script for “GameControl”.
Only difference: how I call it
void Start () {
Save.onClick.AddListener(() => { GameControl.control.Save(); });
Load.onClick.AddListener(() => { GameControl.control.Load(); });
}
----------------------------------------------------------------------------
So why is this happening now? How can I fix it?