[kind of solved] Detect OnTriggerEnter without any script on the trigger or the colliding object?

Hello there,

from what I have tested and read sofar every variant had to have a script on either one of the colliders. So I just wondered if it is possible to detect a collision between two colliders without having any script on any of the colliders but instead have the script on a third object.

Cheers
Ralf

Not really…

If you have a rigidbody, scripts attached to it will activate OnTrigger and OnCollision functions for collides attached to child objects, but not if the colliding objects both have the same rigidbody activating those collisions.

(Edit) You could also try not using colliders, and instead check the positions of the objects in an Update() function to see if they’re close enough to be considered touching.

Why are you trying to avoid attaching a script to the objects?

There is no event that you can add a listener or anything to, unfortunately. If you want this functionality, you can just make a script called like CollisionEvent or something and make events for each of the collisions and triggers, then fire them in the respective OnTriggerEnter/Stay/Exit functions, OnCollisionEnter/Stay/Exit functions, and possibly even the 2D versions. It’s a trivial thing, and would work without modifications every time.

Unfortunately, it still wastes a tiny bit of time on calling the events without listeners, so you may want to do like I did and make them into separate scripts.

SendMessage and related functionality that rely on special function names are pretty ugly- I really hope they move to a more delegate/event-oriented system eventually.

@MSplitz-PsychoK :
Thanks for the check distance idea, which would work perfectly fine for spheres but unfortunately not for objects with different aspect ratios.
Why the hassle you ask? Well I would like to have all scripts of the entire scene on one object as they are easier to find then. But if there is no easy way (performance and handling wise) arround this I will stick to the Unity way :stuck_out_tongue:

@DonLoquacious :
Thanks for your evaluation. I do not completely get how your approach works but it sounds a bit complex for my designers brain :smile: What I do get is that it is impractical from a performance standpoint. So I guess I will stick to the unity way and have the OnTrigger events on the colliders themselves.