Specific Gameobject Trigger

So, I got a script that detects if a gameobject called Player enters it, yes.

void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player"){
            anim.SetBool("PlayAnimation", true);
    }

This works entirely fine, but the problem I am having is that I want the script to reference multiple colliders.

Like a
void OnTriggerEnter(Wall.Collider other)
void OnTriggerEnter(Floor.Collider other)
void OnTriggerEnter(Ceiling.Collider other)

In the same script. Rather than copy a bunch of scripts with one instance of a trigger event in each.
Is this possible? (Pardon the newbieness)

you can have multiple if statements in OnTriggerEnter function, like this:

void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
anim.SetBool("PlayAnimation", true);
}
if (other.gameObject.tag == "Wall")
{
anim.SetBool("PlayAnimation", true);
}
}

you could also use the ‘or’ operator like this(using CompareTag here because it seems like the better way to check tags):

void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player" ) || other.CompareTag("Wall"))
{
anim.SetBool("PlayAnimation", true);
}
}

also read about layers and see if it’s something you wanna use for your situation instead of tags

2 Likes

Yup!
Simply have it check the tag of the colliding object, and have it perform the corresponding action.
For example:

        public void OnTriggerEnter(Collider col) {
            // Grab a referance to the triggering object's
            // tag.
            string tag = col.tag;

            // Checks the tag of the triggering object
            // and compares it to the cases below.
            switch (tag) {
                case "Player":
                    // Player related code
                    break;
                case "Wall":
                    // Wall related code
                    break;
                case "Door":
                    // Door related code
                    break;
                case "Ceiling":
                    // Ceiling related code
                    break;
                    // ETC
            }
        }
1 Like

Drat!
You beat me too it. :slight_smile:

I think you or I misunderstand, it’s not about having multiple “Ifs” attached to 1 collider.
It’s about having rules for multiple colliders in 1 script.

It’s not about 1 gameobject having rules for 10 different things touching it in a script.
It’s about having rules for 10 different colliders in a script that all do 1 thing when “Player” comes in contact.

Thanks for the info so far, guys :slight_smile:

1 Like

Well, each object would need its own script then, as well as its own Trigger collider.
If you want it all in a single script, where you don’t have to create a script for each object, then switch my above code to check the object’s tag (the object that has the trigger, not the object doing the triggering).
Like so:

public void OnTriggerEnter(Collider col) {
            // Grab this trigger's tag.
            string tag = gameObject.tag;
            // Compares this object's tag with those below.
            switch (tag) {
                case "Player":
                    // Player related code
                    break;
                case "Wall":
                    // Wall related code
                    break;
                case "Door":
                    // Door related code
                    break;
                case "Ceiling":
                    // Ceiling related code
                    break;
                    // ETC
            }
        }

But you would still need to put this script on each object, and there really is no way around it.
No matter what you do, you will need to put a script on an object with a trigger, either to just detect that it has been triggered and send this information to another script to deal with, or to handle it’s own trigger events.

That said, do the above, and you will not need to make a script for each object, you will just have to put the script on each object.

To put it simply, in order to detect a trigger, a script with an OnTrigger event must be present on the object with the Trigger collider. You cannot have an outside script detect when another game object’s trigger has been tripped.

EDIT:
Love your little diagram. :slight_smile:

Hehe, thanks. At my laptop, only had paint and had to make due.

At any rate, thanks for your clarification, really. I’ve revised the script and works better than intended with a prefab structure with public properties. (It’s for a system in which the camera moves depending on your location)

Thanks, lads! I really love this forum some times.

1 Like