x


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)

more ▼

asked Apr 18 '11 at 06:12 PM

pepefirst gravatar image

pepefirst
30 11 14 21

As you have an if statement, can you confirm that the tag check is working? Does appleCollect.points increase as you touch the apples?

Apr 18 '11 at 06:30 PM Toxic Blob

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.

Apr 18 '11 at 06:30 PM Toxic Blob

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.

Apr 18 '11 at 06:50 PM pepefirst

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

Apr 18 '11 at 07:10 PM Toxic Blob
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Apr 18 '11 at 06:58 PM

Bampf gravatar image

Bampf
5k 8 19 49

That was the problem: I forgot to add a rigidbody to the apples prefab. Thanks a lot! Also thanks to Toxic Blob!

Apr 18 '11 at 07:23 PM pepefirst
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2497
x336
x227

asked: Apr 18 '11 at 06:12 PM

Seen: 4217 times

Last Updated: Apr 18 '11 at 06:42 PM