Jump to content
  • Advertisement
Sign in to follow this  
nickme

unity:how to add OnTriggerEnter to a prefab

This topic is 2082 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!