Seems like OnTriggerEnter is giving a couple people problems tonight… Hopefully we can work that out, huh?
Essentially, my question is this: Can the OnTriggerEnter() method take in a specific parameter, and not just ‘other’? As in:
void OnTriggerEnter (Collider coconutTreeCollider){
Debug.Log("KickScript hit a Coconut Tree");
}
vs. the generic:
void OnTriggerEnter (Collider other){
Debug.Log("KickScript hit something");
}
That first bit of code didn’t work… Or I wouldn’t be here My logic came from where the documentation says “OnTriggerEnter is called when the Collider other enters the trigger.” So I had hoped that ‘other’ could be replaced with a specific collider.
I want to attach a single script to the main character, who kicks things. I’d like this script to be able to figure out what it is that he kicked, so that different things can happen. At the moment, I have scripts on the kickable objects themselves, but this seems a little impractical and messy. I’d rather not apply the same script to all of the coconut trees coconuts dodos on the island if instead I could do it once to the character.
Those functions are identical, all you’ve done is changed the argument name which has no functional effect.
Regardless, what you’ld need to do in the function is work out what it is you’ve hit (via the object’s name, tag, or some component or attribute of the object), and then deal with them appropriately.
Ah, ok. So ‘other’ changes to suit whatever it is that collided with my trigger. If that happens to be my coconutTreeCollider, than it can use whatever attributes that collider has. I think I have it. Thank you
Oh one more thing - what if there are multiple colliders within the trigger when the function is called? Can I account for that? Or will it just use the first one or some such thing?
P.S. Just tried and it worked perfectly. Such a great feeling Thanks again