From what I’ve read in these threads and the documentation, it seems to me that if you want to use messages that are sent from colliders (OnTrigger…), you need to have a script attached to each game object that has a collider. Is that true? It seems a little messy.
(The alternative that I would prefer is to use an array of colliders, in a single script, on a parent object, and check for triggering for the colliders in the array.)
The trigger message is only sent to the object with the trigger collider directly attached, yes. If you had four trigger colliders as children with a parent object that had a script, you’d never get an OnTrigger event called.
Thank goodness this doesn’t apply for non-trigger children colliders.
In C# you can solve this by attaching a tiny script ( TriggerCallback.cs) to each of your triggering objects, which calls a definable function in your central gamehandling code. Using a delegate this is quite easy. You only have to attach the script to the Triggers and in your main code assign a function (same Type!) to the callback delegate.
Since i’m new to C# scripting and relatively new to Unity itself, there might be place for improvement. Please post if someone sees a flaw in my script or idea ;-). But besides of that i used this method recently and it worked quite well for me…
Pseudocode - assume cbscript holds reference to the attached “TriggerCallback.cs”
Very interesting, I was thinking delegates could be helpful in trigger/collision handling.
However, my C# and Unity knowledge aren’t really up to this.
Did you get this working ?
Anybody else who can help out with some working code ?