Sign in to follow this  
nickme

unity:how to add OnTriggerEnter to a prefab

Recommended Posts

hi

i am trying to add a OnTriggerEnter function to a prefab. but when i did it, the OnTriggerEnter was never called. the OnTriggerEnter script is attached to a car prefab. is that a problem? i check and recheck the spelling and syntax but nothing seem to works. the script is as folllow. i did added a tag to the car collider which is called duck. the print statement was never executed.

thanks

#pragma strict
function OnTriggerEnter(col : Collider) : void
{
print(" collides duck");
if (col.gameObject.tag == "duck")
{
gameObject.SendMessage("AddDuckCount", 10);
Destroy(col.gameObject);
}
} Edited by nickme

Share this post


Link to post
Share on other sites
hi

i improved the program a little bit by attached the script in my last post to the car prefab instead of "chassis collider" and now the OnTriggerEnter executed. but there is another problem: the OnTriggerEnter function catched the other objects but not the Duck. as i memtion above, i'd added a box collider to Duck.

it seem that the chassis collider only recognizes a collider whose gameObject is not a Prefab. it recognizes finishLine and DuckRelease but not Duck and cone1. how can i get around that?

i am still learning.

the AddDuckCount() now looks like this:

#pragma strict
function OnTriggerEnter(col : Collider)
{
print(" car collides with something.");
if (col.gameObject.tag == "Duck")
{
print(" collides with a duck by car.");
gameObject.SendMessage("AddDuckCount", 10);
Destroy(col.gameObject);
}
else if (col.gameObject.tag == "cone")
{
print(" collides with cone by car.");
gameObject.SendMessage("AddDuckCount", -5);
}
else print(" not duck, but: " + col.gameObject.name);
}

maybe someone can suggests what should i look for.

thanks Edited by nickme

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this