0
votes

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. whale inspector whale inspector

cube inspector

3
Add also the inspector for the fishes - Ignacio Alorre
This flock.cs script you're showing us isn't attached to the cube gameobject in the inspecter window you're showing us. So I'm assuming it's on some other object, guessing the fish that we can't see. Showing us the cube having a collider(trigger enabled) and rigidbody does nothing because the fish/school/object the script is attached to is what needs those things. Also, the way you have the script will flip the fish's turning status to true whenever it touches ANY object, is that desired behavior? - oxrock
@oxrock correct. that's what I want to implement. - Kwang Won Choi

3 Answers

1
votes

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)

4
votes

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.

0
votes

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. :)