- Home /
OnTriggerEnter do not triggers
I wrote this simple script to an i-pad game in which my Player should pick-up automatically apples (when Player and apple enter in touch) from the floor and add points to the score for that. I attached it as a this separate script to my Player and checked the Trigger marks, both in the apple and the Player. Although the easy this seems, the apple do not disappears; that is, I missed something I can't find. Any help, please.
var points : int = 0
function OnTriggerEnter(collisionInfo : Collider){
if(collisionInfo.gameObject.tag == "apple"){
points++;
audio.PlayOneShot(appleCollect);
Destroy(collisionInfo.gameObject);
}
}
@script RequireComponent(AudioSource)
As you have an if statement, can you confirm that the tag check is working? Does appleCollect.points increase as you touch the apples?
Also, on the iPad you want to avoid using Destroy as that causes garbage collection and memory reallocation. It's better to simply disable the apple, then re-enable it when needed elsewhere.
Sorry Toxic Blob, I made an edit to my post after your answers because I got a Unity error message with Collision Points; I changed it by var points. I have not implemented yet the Scorer script but I suppose it will not work either because I do not get the pick-up sound.
Try below points++ adding "Debug.Log("collision with an apple");. If you don't get that then either the tag comparison is failing, or you need a rigidbody as Bampf suggests below
Answer by Bampf · Apr 18, 2011 at 06:58 PM
Probably you need to add a rigidbody to one or both of the objects. From the documentation for Box Collider:
Be aware that in order for two Triggers to send out trigger events when they collide, one of them must include a Rigidbody as well. For a Trigger to collide with a normal Collider, one of them must have a Rigidbody attached. For a detailed chart of different types of collisions, see the collision action matrix in the Advanced section below.
The same is true for the other collider types.
That was the problem: I forgot to add a rigidbody to the apples prefab. Thanks a lot! Also thanks to Toxic Blob!