if (other == someCollider) { do Stuff; } Not being called

After looking at this post, I decided to declare colliders in the constructor and change my scripts to find them. This has not worked.

My code is -

public Collider collider1;

void OnTriggerEnter(Collider other)
    {
        if (other == collider1)
        {
            print("A check to see if Trigger Enter has been called");
        }
     }

The code is not creating errors. I have correctly selected the collider in the inspector, which is set to Is Trigger.

I would appreciate if somebody could possibly tell me where I am going wrong. Thanks.

other needs a rigidbody.

Other has a rigidbody.

Are you dragging a prefab or scene object into the inspector for collider1?

If it’s the former, then that’s the problem as you would be performing the comparison against a prefab rather than an instance.

Could I ask why you aren’t using something like tags for the comparison check?

1 Like

I am using tag comparison checks. I have simplified my code to show the part which is not working.

The full method is -

void OnTriggerEnter(Collider other)
    {
        if (other == queueBox && other.gameObject.CompareTag("BadGuy"))
        {
            animator.SetBool ("Range", true);
            animator.SetBool ("InQueue", true);
            nav.Stop();
            queueSkeleton = other.gameObject;
        }

Before this point, I had -

void OnTriggerEnter(Collider other)
    {
        if (other is BoxCollider && other.gameObject.CompareTag("BadGuy"))
        {
            animator.SetBool ("Range", true);
            animator.SetBool ("InQueue", true);
            nav.Stop();
            queueSkeleton = other.gameObject;
        }

And it worked perfectly. Now that I require more than one BoxCollider, I have needed to name them. By naming them, they aren’t being called.

I am dragging the BoxCollider within the inspector onto the blank box that asks for queueBox (collider1 in the first example I used).

I am finding that having more than one BoxCollider is vital at this stage and really want to know how to create, assign and access them properly.

@Munchy2007 , you mention another way (not dragging) to assign the Collider. I would be grateful if you let me know how this is done.

Dragging from the Project hierarchy or the Scene hierarchy?

The code seems to work fine for me.

1 Like

Box Collider at the bottom is dragged into Queue Box, which is beneath the Points Array. All is in the Inspector. I don’t know what is meant by ‘Project hierarchy’ or ‘Scene hierarchy’. But in this case, it would only apply to this particular scene.

Triggers do not trigger other Triggers.

Not quite true. A trigger message happens if:

  • Two objects with colliders intersect
  • One of the objects that intersect has a rigidbody
  • One of the objects that intersect has it’s collider set to trigger

@OP: What type have you declared the Queue box as? If the type is Collider, you might have assigned the sphere collider you want for the attack sphere in the queuebox slot.

If that’s the case, you can drag the box collider directly from the inspector instead of dragging it in from the hireachy - just click on the “Box Collider” text, and drag into the Queue Box slot.

By the way, you don’t need to check the tag. You’re checking if it’s exactly one collider, which means that the tag is already a given.

He’s dragging the Box Collider[Trigger] on the same GameObject shown in the pic to be the queueBox, then expecting OnTriggerEnter to be fired when a Trigger enters it which does not happen.

queueBox needs to not be a Trigger if it is intended be used to activate a different Trigger.

@Baste , I have tried declaring Queue box as a BoxCollider and a Collider before now and both times I haven’t had success. I am dragging the item in the Inspector onto the slot. If you’re able to see in the small image, the cuboid shaped green image is in the Queue box slot, while the spherical shaped image is in Attack Sphere. My object has no other sphere/box colliders.

@LaneFox , If the colliders aren’t set to Is Trigger, then the objects that they are looking for do not intersect, as they’re solid. Nothing is happening when these solid colliders hit what they’re looking for.

Are there any images/more information that I can provide, as I suspect I haven’t provided enough.

If you could throw together a small example project showing what you’re trying to do not working, I could take a look and tell you what you’re doing wrong.

I don’t have enough time today to produce any example project, I have however recorded a 12 second video of the problem taking place.

This video looks at AttackSphere. The code for AttackSphere is -

void OnTriggerEnter(Collider other)
    {
if (other == attackSphere && other.gameObject.CompareTag("Player"))
        {
            print ("Found attackSphere!");
        }
    }

The above was used to replace the following code -

void OnTriggerEnter(Collider other)
    {
if (other is SphereCollider && other.gameObject.CompareTag("Player"))
        {
            animator.SetBool ("Range", true);
            animator.SetBool ("InQueue", false);
            nav.Stop();
            gamer = other.gameObject;
            print ("Found attackSphere!");
        }
    }

Which worked perfectly.

I appreciate the help given thus far and think I could make an example project tomorrow if still required.

It looks like I was wrong, Triggers can make an OnTriggerEnter fire if they also have a Rigidbody.

Here is a small test scene with some scenarios of possibilities.

2374835–161427–triggers.unitypackage (3.51 KB)

1 Like

Wait, attackSphere is on the skeleton footman, right? But you’re checking if the collider you collider with is the attack sphere, and has the tag “Player”. Those two things don’t seem to me like they would the true at the same time.

What are you trying to do with that if-check? Are you checking if you have collided with the player character? Or if you have collided with the enemy?

1 Like
    void OnTriggerEnter(Collision col){

        // if an object collides with an tag

        if (col is SphereCollider && col.gameObject.tag == "") {
            // your code
        }

        // or an object collides with an name

        if (col is SphereCollider && col.gameObject.name == "") {
            // your code
        }

    }

maybe try this but I’m not sure…

I have it working now. The package from @LaneFox was really useful and I immediately saw my fault. Thanks @Baste , for again helping me out with an issue, your words made a lot of sense.

The collider was attached to the Skeleton, while I said other should have the tag of Player (a separate object). Having one statement as true, and the other untrue was the reason it wasn’t being called.

When declaring the AttackSphere should be on the Player, everything works and compare tag is needless.

I do question how it is possible to use more than one collider type on the same object properly. I just added an extra Sphere Collider to the player, set it to is trigger and I cannot see any way of distinguishing one from another in the inspector, in fact only one collider is being shown. Can anyone answer how to use multiple colliders of the same type (Box, Sphere, etc.) on a single object?

Edit:
Child objects with different colliders would suffice, and I am guessing that is how it is done.

You can have all sorts of colliders on the same GameObject. To link others than the top one in the inspector stack: Create another inspector tab that you lock, then drag and drop.

A quick note here: colliders on child objects will send trigger messages to scripts on the parent object if:

  • The parent object has a rigidbody
  • The child object doesn’t have a rigidbody.

This allows you to build what’s called a compound collider, which there’s information about here.