Why is this simple cast not working?

EventManager class:

public class EventManager : MonoBehaviour
   {
       public event EventHandler DialogEvent;

       public static void ProcessEvent(IEvent eventObject)
       {          
           EventManager eventMgr = ManagerRepo.Instance.EventMgr;
           switch (eventObject.Type)
           {
               case EventType.Unknown:
                   break;
               case EventType.Dialog:
                   eventMgr.DialogEvent(eventObject, EventArgs.Empty);
                   break;
               default:
                   break;
           }
       }
   }

And the handling method in another class:

private void HandleDialogEvent(object sender, EventArgs e)
       {
           NpcResponse response = sender as NpcResponse;
           Debug.Log("Event received for keyword: " + response.Keyword);
       }

The cast within the HandleDialogEvent method is not working and the variable response ends up being null.

Please check the attached screenshot for more details.

As will return null if the object can’t be cast to the appropriate type. So always use a null check when using As.

If you know the type in advance, it’s better to use explicit casting. Or even better, properly type the argument coming in.