问题
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