- Home /
Boo Events
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?
Answer by Jaliborc · Jan 22, 2012 at 10:38 PM
After much trial and error, I found the solution myself:
(Events.boo)
class Events:
static public event GameOver as callable()
static public def FireGameOver():
GameOver()
(SomeCollider.boo)
import UnityEngine
class SomeCollider (MonoBehaviour):
def OnCollisionEnter (collision as Collision):
Events.GameOver += Test
Events.FireGameOver()
def Test ():
print('Test')
I could not, however, find a solution that would avoid the need to define a function for each event, as they appear to not be callable outside of the class methods.
Your answer
Follow this Question
Related Questions
Coroutine couldn't be started! [BOO] 2 Answers
Is it wise to add scripts to Unity's Event System? 0 Answers
Best way to handle multiple buttons? 2 Answers
Chain UI animations with Events? 1 Answer
OnSceneGUI mouse Dragging 0 Answers