- Home /
NullreferenceException with an Event
Hello everyone.
Something in my code has been bugging me for a couple of days. I can't seem to get it to work, I'll post some code and explain my problem:
//C#
public class Foo : MonoBehaviour {
public delegate void EventHandler();
public event EventHandler InitComplete;
public void Init() {
//Many Irrelevant stuff goes on here
InitComplete();
}
}
So this is the code I've got. Whenever I play the game in the editor, I get a NullReferenceException at the line where I call to trigger the event InitComplete(). Telling me the usual message "Object reference not set to an instance of an object".
As I don't fully understand how events work. I can't seem to get what's going on here. If it isn't obvious with the code, I should say that I'm trying for the class to throw an event called InitComplete, at the end of the method Init().
What I find weird is that everything works fine except that event. No null references anywhere else. Actually, the class which is triggering the event has instances (and even if it didn't it shouldn't make a difference, simply the event wouldn't get triggered, wouldn't it?).
I'm kind of confused with all this. I'd appreciate some help.
Thank you very much.
Answer by CHPedersen · Jan 14, 2013 at 02:37 PM
This is because you have to check if the event is null before you trigger it:
if(InitComplete != null)
InitComplete();
The reason is that events are actually placeholders for a list of delegates to be called whenever the event is triggered. That's why you can use "+=" when you subscribe to an event - you're adding your handler to a list of methods that are going to be called. But if nobody has subscribed yet, (there is nowhere in your code where it says Foo.InitComplete += SomeHandler;) then the list of delegates to call will be empty, and trying to access it therefore causes a null reference exception.
That explains it, I'm going to fix it and post with some feedback. Thank you!
EDIT: It worked, once again thank you. I'll mark this as answered
Answer by pierrickian · Apr 18, 2014 at 12:45 PM
Hello, I encountered the same problem but the fact is I managed many other events that work well. But one of them is null (OnPlayersAroundTable) even if some gameObjects have subscribed to it. Could someone please help me ?
My code below:
using System;
using System.Collections.Generic;
using UnityEngine;
public class MyClass : MonoBehaviour
{
public static List<int> playersTable;
public delegate void PlayersAroundTable (List<int> PlayersTable);
public static event PlayersAroundTable OnPlayersAroundTable;
public static void awake () {
playersTable = new List<int> ();
playersTable = GetPlayersTable ();
OnPlayersAroundTable (playersTable);
}
}
public class MyOtherClass : MonoBehaviour
{
public List<int> playersTable;
void Start () {
MyClass.OnPlayersAroundTable += InitPlayersAroundTable;
}
public void InitPlayersAroundTable (List<int> PlayersTable)
{
playersTable = PlayersTable;
}
}
If the method 'OnPlayersAroundTable (playersTable);' is in the 'void Start ()' of MyClass class, it works ! Someone can explain why ?
Pretty sure help is not still needed, it might be because of the wrong case of awake() method. Use Awake() ins$$anonymous$$d. And please don't post any followed up question as an answer. Use comments like this for that.
Your answer
Follow this Question
Related Questions
Call parent guitexture mouse events 0 Answers
Detect when the Player starts 1 Answer
Can you get mecanim to not play events when animation isn't playing? 0 Answers
Unity hardware changed event 0 Answers
Custom Inspector: Unity Events 2 Answers