- Home /
OnTriggerEnter for a collider attached to another gameObject
I need to check when an object enters a collider attached to another game object. I have set the collider through the inspector as a public variable. I tried using the standard onTriggerEnter function but passing the other collider in, however this doesn't seam to work.
Is there a way to detect gameObjects entering colliders attached to other game objects?
public SphereCollider theCollider;
// Check if GameObject enters
void OnTriggerEnter (Collider theCollider) {
//Do Stuff
}
Could you add that as an answer so I can accept it? it was the best way to do it.. the other guy who answered doesn't seam to have understood the question...
Answer by MrSoad · Oct 27, 2014 at 12:49 PM
Hi, I would have the OnTriggerEnter(or collision variant) function attached to the object that you want to detect the event on. When triggered call a function on the script/object you need this information for. Create a reference to the main script at the start of your collider scripts.
Answer by RedDevil · Oct 27, 2014 at 12:39 PM
void OnTriggerEnter (Collider theCollider)
{
if(col.tag == "tagname")
{
//do stuff
}
}
you need to set a tag name to the object you want to collide with your collider. If you have allot of collisions i suggest using switch instead of if statements.
I want to detect everything that collides with the collider, so the tag thing won't help much. $$anonymous$$y problem is that this approach doesnt seam to work when trying to detect thing from a collider attached to another game object...
if you want it to do the same thing when it hits anything that has a collider then remove the if statement and select from the inspector IsTrigger
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Weird behaviour with OnTriggerEnter 0 Answers
How to do collision in 2d game? 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer