I am trying to create a singleton class for handling events, which could then be registered and fired by other classes.
Here is my code so far:
import UnityEngine
class Events (MonoBehaviour):
static public Handler as Events
public GameOver as callable()
def Awake ():
if not Handler:
Handler = self
def FireGameOver ():
GameOver()
However, trying to add a method to the event fails with error “Object reference not set to an instance of an object”. Here is my testing class:
import UnityEngine
class SomeCollider (MonoBehaviour):
def OnCollisionEnter (collision as Collision):
if collision.gameObject.tag == 'MakeTest':
Events.Handler.GameOver += Test
Events.Handler.FireGameOver()
def Test ():
print('Test')
def Test2 ():
print('Test2')
Also, is there any way to avoid the need for the FireGameOver
method? Or any easier method to implement this approach?