No Event.current inside an event handler?

Hello,

I’m new to Unity and have encountered something of a puzzle (to me anyways).

I have a script with a function like this:

void OnMouseUp()
    {
        Debug.Log("Current detected event: " + Event.current);
        Event e = Event.current;
        if (e != null)
        {
            Debug.Log("event on " + collider + " was of type: " + e.type);
        }
    }

But the log shows only “Current detected event:”, and that just doesn’t make sense to me.

Why doesn’t the Event.current attribute show, when I’m actually inside a handler for an event?

Also, I would like to ask how/if I can make a ‘global’ event handler. Events seem to be all tied with colliders or GUI elements, which is fine and well, but is there some GameObject I could attach a script to, which would then react to any Events on the entire screen?

Thanks in advance,
-Tobias

IIRC, the Event class is only usable from inside OnGUI.

Indeed - Event is only available in OnGUI(). Someone with more internal engine knowledge can correct/elaborate here but I believe these are not hooked as true “events” and therefore the standard patterns don’t apply.

I see. Thanks a bunch :smile:

Any tips on how to attach a script to an entire scene, like some sort of OnGUI call for the scene as a whole? I know the OnGUI and Update is called on every script, but for architectural reasons it would be nice to have such ‘global’ behavivour in one place, rather than on some arbitrary GameObject.

Thanks again!

The general pattern is to create an empty GameObject and attach such “management” scripts to it. That’s really the only way to “attach a script to a scene”. There is nothing saying that your processing code needs to be in a MonoBehaviour inherited class attached to a GameObject, however you would probably still need to use the above method in order to hook the other class (assuming you wanted to run every frame).

Alright. Thanks for your help KelsoMRK! :slight_smile: