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. Dismiss Notice

Is it possible to Tag a Trigger?

Discussion in 'Scripting' started by graviton, Sep 25, 2014.

  1. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    I'm trying to use Tags for both the Trigger and Game Object entering the Trigger

    e.g.
    If a Game Object Tagged "Blue" enters a Trigger also Tagged "Blue", do something

    Is it possible, can I get a code example?
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    The collider's / trigger's tag is the tag of the gameObject which holds the collider/trigger.

    There are plently examples, simply use the documentation or the forum search. :)
     
  3. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Code (csharp):
    1. void OnTriggerEnter(Collider c)
    2. {
    3.   if (tag == c.tag && tag == "Blue")
    4.     // Do something.
    5. }
     
    graviton likes this.
  4. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    Thanks for the code example

    I should have mentioned that, I'm trying to do all this without having to attached the script to neither the "Trigger" nor the "Game Object" entering the Trigger

    I want to having the actual script attached to an "Empty Game Object" instead, separate from the Tagged "Game Object" and "Trigger"
    (more like doing a "gameObject.Find", where the script doesn't need to be attached to the thing it's controlling)

    e.g.
    Find Trigger Tagged "Blue" and if the Object that enters that Trigger is also Tagged "Blue", then do something
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    The problem is that collisions/trigger events call the collision and trigger method of scripts that are attached to the gameobjects that are interacting with each other.
    With your approach you'd have to keep track of the GO's colliders and do all the collision/trigger math yourself, i doubt you really wanna do that as it sounds pretty easy but can be very complicated.

    You don't really wanna reinvent the wheel, do you? In my opinion there's absolutely nothing wrong with the methods that unity provides, it saves a lot of work. Unity would call these methods either way.
     
  6. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    The thing is, the OnTriggerEnter() message only gets sent to the colliders involved. So you have to attach a script to one of them.

    Prime31's open source character controller has a class that does nothing but listen for trigger and collision messages and pass them on to some third game object that was not involved.
    https://github.com/prime31/Characte...cterController2D/Scripts/CC2DTriggerHelper.cs

    If you don't need to know about every type of trigger/collision event, I might simplify that script even more and only pass the exact data I need.
     
    graviton likes this.
  7. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    Garth
    Thanks again for the help
    If you don't mind, that's would be awesome
     
  8. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    Never mind, I got it :)
     
  9. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    not working