- Home /
Does the -OnTriggerStay collider- script have to be child of collider triggering the function?
Or can you tell it, use a different objects collider as trigger?
Answer by Loius · Jan 22, 2013 at 07:28 PM
No, it has to be on the object which is colliding.
You're free to send messages from the OnTriggerStay function to whatever objects need to know about the collision.
You can even call other OnTriggerStay functions if you need to for some reason, though that might get confusing.
How would you do this? if the message needs to be sent to component
Just call the function like any other function.
script.Function()
Answer by aldonaletto · Jan 22, 2013 at 07:30 PM
I don't know if this is exactly what you're asking, but all OnTrigger events are sent to scripts attached to the trigger object and to the object that entered the trigger, not to their parents or children. If you have a script attached to object A and want to detect when something entered the trigger in a child of A, a simple solution is to use SendMessageUpwards to make the trigger call a function in its parent - add the code below to the child trigger script:
function OnTriggerEnter(other: Collider){
SendMessageUpwards("TriggerEntered", other);
}
Define the function TriggerEntered(other) in the parent object's script:
function TriggerEntered(other: Collider){
// the collider "other" entered the child trigger
}
I am trying to send message from item collider to player script, that it has entered that collider. Is this a built in function that I would not need to send a message? And maybe a way to say, ok, the player has entered the collider of certain objects collider? Trying to understand how it all works..
If you want to send OnTrigger events to the player when he enters a trigger, just add the OnTrigger function to the player script, like this:
function OnTriggerEnter(other: Collider){
print("Player entered "+other.name);
}
But the player must be a CharacterController or Rigidbody in order to be detected. If the player is a simple object with a collider, add a kinematic rigidbody to the trigger or to the player. Another hint: if the trigger moves, you must add a kinematic rigidbody to it, or it won't detect other colliders.