Do C# events work in unity?

I have got one gameobject with a script attached which exposes 2 events, LevelLoaded and LevelUnloaded, the ValueEventHandler is just an extended EventHandler which accepts a type for the event args to carry over.

public class CurrentLevelBehaviour : MonoBehaviour
{
    public event ValueEventHandler<ILevel> LevelLoaded;
    public event ValueEventHandler<ILevel> LevelUnloaded;
    // Methods which raise events
}

Then I have another gameobject with a behaviour which is a HUD of sorts, so when a level is loaded it needs to know to update and display the information, however for some reason the callback is never raised.

public class LevelHud : MonoBehaviour
{
    private ILevel _currentLevel;

 // Use this for initialization
 void Start ()
 {
     var currentLevelBehaviour = GameObject.Find("CurrentLevel").GetComponent<CurrentLevelBehaviour>();
        currentLevelBehaviour.LevelLoaded += (sender, eventArgs) => { _currentLevel = eventArgs.Value; };
        currentLevelBehaviour.LevelUnloaded += (sender, eventArgs) => { _currentLevel = null; };
 }
 
 // Other methods which use _currentLevel
}

So is this a unity problem? as there are no errors from the compiler or from unity when running things, I can just see that the level is not being set in the HUD area.

The simple answer is yes, .NET events work in Unity. I use them widely in my game.

What might be at issue is when your events are getting raised. Empirically, I’ve observed that OnLevelWasLoaded() executes before either Start() or Awake(). If CurrentLevelBehavior raises the events from OnLevelWasLoaded(), your handlers have not yet been added to the events. I’ve found judicious use of Debug.Log() to be a good way to diagnose these ordering issues.