Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Specific Gameobject Trigger

Discussion in 'Scripting' started by Littlenorwegian, May 23, 2016.

  1. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    So, I got a script that detects if a gameobject called Player enters it, yes.
    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2.     {
    3.         if (other.gameObject.tag == "Player"){
    4.             anim.SetBool("PlayAnimation", true);
    5.     }
    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)
     
  2. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    you can have multiple if statements in OnTriggerEnter function, like this:

    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2. {
    3. if (other.gameObject.tag == "Player")
    4. {
    5. anim.SetBool("PlayAnimation", true);
    6. }
    7. if (other.gameObject.tag == "Wall")
    8. {
    9. anim.SetBool("PlayAnimation", true);
    10. }
    11. }
    12.  
    you could also use the 'or' operator like this(using CompareTag here because it seems like the better way to check tags):

    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2. {
    3. if (other.CompareTag("Player" ) || other.CompareTag("Wall"))
    4. {
    5. anim.SetBool("PlayAnimation", true);
    6. }
    7. }
    also read about layers and see if it's something you wanna use for your situation instead of tags
     
    TonanBora and KelsoMRK like this.
  3. TonanBora

    TonanBora

    Joined:
    Feb 4, 2013
    Posts:
    493
    Yup!
    Simply have it check the tag of the colliding object, and have it perform the corresponding action.
    For example:


    Code (CSharp):
    1.         public void OnTriggerEnter(Collider col) {
    2.             // Grab a referance to the triggering object's
    3.             // tag.
    4.             string tag = col.tag;
    5.  
    6.             // Checks the tag of the triggering object
    7.             // and compares it to the cases below.
    8.             switch (tag) {
    9.                 case "Player":
    10.                     // Player related code
    11.                     break;
    12.                 case "Wall":
    13.                     // Wall related code
    14.                     break;
    15.                 case "Door":
    16.                     // Door related code
    17.                     break;
    18.                 case "Ceiling":
    19.                     // Ceiling related code
    20.                     break;
    21.                     // ETC
    22.             }
    23.         }
     
    jaasso likes this.
  4. TonanBora

    TonanBora

    Joined:
    Feb 4, 2013
    Posts:
    493
    Drat!
    You beat me too it.
    :)
     
  5. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    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 :)
     
    TonanBora likes this.
  6. TonanBora

    TonanBora

    Joined:
    Feb 4, 2013
    Posts:
    493
    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:


    Code (CSharp):
    1. public void OnTriggerEnter(Collider col) {
    2.             // Grab this trigger's tag.
    3.             string tag = gameObject.tag;
    4.             // Compares this object's tag with those below.
    5.             switch (tag) {
    6.                 case "Player":
    7.                     // Player related code
    8.                     break;
    9.                 case "Wall":
    10.                     // Wall related code
    11.                     break;
    12.                 case "Door":
    13.                     // Door related code
    14.                     break;
    15.                 case "Ceiling":
    16.                     // Ceiling related code
    17.                     break;
    18.                     // ETC
    19.             }
    20.         }
    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. :)
     
    Last edited: May 23, 2016
  7. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    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.
     
    TonanBora likes this.