Unity - OnTriggerEnter is not being called

痴心易碎 提交于 2021-02-20 02:58:08

问题


I'd like to make an aquarium where diverse fishes are existing.

I tried not to let fishes pass through cubes, but the OnTriggerEnter function doesn't work even if objects enter the trigger.

These pictures represent each of whale and cube inspector factors.


回答1:


You only need to add a Collider to your fish, (at the same level of the Flock script, or on a child GameObject).

OnTriggerEnter will only fire when a Collider enters a Trigger Collider

See MonoBehaviour.OnTriggerEnter(Collider)




回答2:


I guess you are missing some of these things:

  • Add RigidBody to the fishes
  • Set the Collider of the GameObject that will detect the fishes as is trigger. And leave it unchecked in the Collider of the fishes

EDIT:

From your latest screenshot, your flock script that is supposed to detect the trigger is not attached to your GameObject. Simply attach it to one of the two GameObjects that is supposed to collide together.




回答3:


Ok so I've had this problem before. Something that helped me A LOT was to MAKE SURE YOU CREATE YOUR SCRIPT IN YOUR OBJECT. It's just easier.

If you didn't create your script IN your object then when you typing void OnTriggerEnter, there won't be any suggestion to finish the word 'OnTriggerEnter'. If you create it in the object than it should suggest to finish the word as 'OnTriggerEnter'.

Or you're just missing a collider or rigidbody. :)



来源:https://stackoverflow.com/questions/46313824/unity-ontriggerenter-is-not-being-called

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!