- Home /
Intercept Collider 2D/Collision2D events with a single script on scene
Hello,
It is possible to intercept all events happening in the scene through a single script?
Thinking about the structure .... inside the tree we would have one GameObject
with the script (InterceptEvents.cs) that will intercept any event that the objects in the scene can trigger.
For example:
public class InterceptEvents : InhertsFromColliderLib
{
void OnTriggerEnter2D(Collider2D other) {
Debug.Log("OnTriggerEnter2D");
}
void OnMouseEnter () {
Debug.Log("OnMouseEnter");
}
}
As this example of Unity: https://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html (NOTE: Attaching this component to a GameObject will make that object intercept ALL events, and no events will propagate to parent objects.)
Thus we do not need to add a script to all objects that need monitored events.
That would be a good practice?
Thanks in advance!
Your answer
Follow this Question
Related Questions
Have 2 colliders on same trigger (and hit the first to enter) 0 Answers
Setting a Prefab Clone as the Child of another Object on Collision (2D) 1 Answer
What other reasons would a 2D collider not make contact with another 1 Answer
OnCollisionEnter is not working for me!! 1 Answer
Prevent collision between prefab clones on a 2d mode? 0 Answers