- Home /
I would like a GameObject to have both a Collider and a Trigger
I have a goal area which I want to be a Collider (working fine) and a Trigger (so I can take some action now another object has entered this area.
I added 2 Polygon Collider 2D components to my GameObject one which is a collider and one which is not. I add a script to the GameObject with the following code:
public class GoalListener : MonoBehaviour {
void OnTriggerEnter(Collider other){
print ("GOTCHA");
//Destroy (other.gameObject);
}
void OnTriggerExit(Collider other){
print ("OH YOU EXITED");
//Destroy (other.gameObject);
}
void OnTriggerStay(Collider other){
print ("YOU ARE NOW IN STAY");
//Destroy (other.gameObject);
}
}
But for some reason my OnTriggerEnter, Exit and Stay never fires. The object which enters the trigger has a RigidBody2D and a Circle Collider 2D.
If the object has a collider, why does it need a trigger or are they different regions of the object?
$$anonymous$$aybe it doesnt, but if it doesnt - what callbacks get called when something collides within the object? How can i interrogate the state of the object hitting the collider to deter$$anonymous$$e if it has enough 'energy' to move outside of the goal? I basically want a callback when an object enters my goal with a small enough amount of energy such that it cannot bounce back out of the goal.
Answer by TheSituation · Apr 17, 2014 at 06:49 PM
The solution was to use OnTriggerEnter2D(Collider2D other) or OnCollisionEnter2D(Collision2D other) whereas I was using the 3D space equivalents which explains why they were not called.