I have two objects, a cannonball and a target. For some reason (probably something minor that I’m just not seeing) OnTriggerEnter just isn’t recognising when the two collide.
The target has a box collider with ‘is trigger’ checked. The cannonball has a ridigbody and sphere collider.
This is the script -
public class MegonHealth : MonoBehaviour
public static int megonLife = 4;
public Transform bossExplosion;
void OnTriggerEnter(Collider collider)
if(collider.gameObject.name == "CannonBall")
megonLife -= 1;
Instantiate(bossExplosion, transform.position, transform.rotation); // Instantiate Explosion Effect
What am I missing guys ???
Try to give both your objects a rigidbody. You can check “isKinematic” on the rigidbody if you don’t want it to be affected by physics.
I’m assuming both objects have a collider as well.
See if either object is on a particular layer. If they are, then check Project Settings->Physics and make sure that the two layers are able to collide in the Collision Matrix.
Hope that helps.
Oh, also put a debug.log statement inside your onTriggerEnter function before you check for cannonball. Something like "Debug.Log(“Collided with “+other.gameobject.name”);” just to check if you’re colliding with anything at all.
Solution: Check the game object’s tag instead of name. Set a tag for your cannon ball called “CannonBall” and check it in your OnTriggerEnter function.
if(collider.gameObject.tag == “CannonBall”)
I don’t care if this question is old, for anyone who might have my problem, make sure “OnTriggerEnter” is spelled exactly how it’s supposed to be. My “T” in Trigger was lowercase and the console log didn’t tell me because it assumed I was declaring my own custom method instead of using the built in method that is OnTriggerEnter.
Did you check frame by frame that your colliders actually collide. Sometimes if your object is moving very fast it can miss the collision.
i’m not sure but i guess your actual gameobject name did not match with the on in the script (script line 13).
please reply if the problem is solved