I get a NullRefernceException even though I subscribed to the event in an Start Methode.
Where I create my Event:
public EventHandler<CustomArgs> ClickEvent;
private void OnMouseDown()
{
Debug.Log("Clicked");
CustomArgs args = new CustomArgs();
args.Name = gebäude.ToString();
args.Level = Level;
args.MenuePosition = Menue;
ClickEvent?.Invoke(this, args);
}
Where I subscribe to my Event:
private void Start()
{
miene.ClickEvent += ClickEvent;
Debug.Log("Event Addedet");
}
private void ClickEvent(object sender, CustomArgs e)
{
//some useless stuff
}
Events are
nullwhen no-one has subscribed. Fortunately, modern C# makes this easy:With older language versions, you need to be more verbose:
They mean exactly the same thing.
As a small optimisation, you may wish to defer creating the
CustomArgsobject until you know someone cares, though: