- Home /
Alternative to OnTriggerEnter
Hey there, I was wondering if there was an alternative function to the "Function OnTriggerEnter (hit : Collider)" one. In other words, is there another function where when the object the script is attached to is touched, a function is performed? Thanks for your help.
Answer by Bovine · Jul 18, 2012 at 09:04 PM
If I understand you correctly you could do this with an event or send message, the event might look like:
public delegate TriggerFunction(Collider col);
public event TriggerFunction OnTriggerEnter = null;
and then in your trigger method:
void OnTriggerEnter(Collider col)
{
if(OnTriggerEnter != null) OnTriggerEnter(col);
}
Ahh, sorry I see you're using JS, and I am a C# bod. With luck there are equivalents in JS that you already know about, or someone might post the JS who can confidently write it (i.e. not me).
Otherwise it might be easier to SendMessage() within the OnTriggerEnter() method:
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html
You could SendMessage() on the current GameObject or on one supplied as a field in the script. The function to send to could also be supplied as a field.
I don't know for sure but the event method should be more efficient, but that might not matter for your purposes.
Alright, thanks. But as I see it, you can only use one of those functions WITH the OnTriggerEnter function, not ins$$anonymous$$d of it, if you get what I'm saying.
No nothing ins$$anonymous$$d of it to my knowledge, but above is a way you could provide a means of subscribing to the trigger events which would be just as good I$$anonymous$$HO.