Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trigger question

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

  1. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    I have two separate objects, a Game Manager (GM) and a Trigger (MyTrigger)

    I want to know if something enters "MyTrigger"

    The catch is, I don't want the "OnTriggerEnter" script to be attached to "MyTrigger"

    Instead, I want the "OnTriggerEnter" script to be attached to the "GM" object

    NOTE, I mean zero scripts on the Trigger, just on the "GM" object, I don't want to send the Trigger Messages from the Trigger

    Is this possible, it seems like something Unity should be able to do

    Please help, I need a way to do this
     
  2. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    I'm afraid you'll have to have a script on either the trigger or the object entering the trigger. What exactly are you trying to do?
     
  3. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    You could forward it using a sendmessage command.

    Output:
    I attached ForwardTriggerMessages to a cube with a trigger, then applied TriggerReceiver to Main Camera. Then I just set forwardTo to MainCamera.



    ForwardTriggerMessages.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class ForwardTriggerMessages : MonoBehaviour {
    4.     public Component forwardTo;
    5.  
    6.     void OnTriggerEnter(Collider other) {
    7.         forwardTo.SendMessage ("OnTriggerEnter",other,SendMessageOptions.DontRequireReceiver);
    8.     }
    9.     void OnTriggerExit(Collider other) {
    10.         forwardTo.SendMessage ("OnTriggerExit",other,SendMessageOptions.DontRequireReceiver);
    11.     }
    12.     void OnTriggerStay(Collider other) {
    13.         forwardTo.SendMessage ("OnTriggerStay",other,SendMessageOptions.DontRequireReceiver);
    14.     }
    15. }
    16.  
    TriggerReceiver.cs
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class TriggerReceiver : MonoBehaviour {
    5.  
    6.    void OnTriggerEnter(Collider other) {
    7.      Debug.Log ("Entered : " + other.gameObject.name );
    8.    }
    9.    void OnTriggerExit(Collider other) {
    10.      Debug.Log ("Exited : " + other.gameObject.name );
    11.    }
    12.    void OnTriggerStay(Collider other) {
    13.      Debug.Log ("Staying : " + other.gameObject.name );
    14.    }
    15. }
    16.  
     
    Last edited: Sep 30, 2014
    graviton likes this.
  4. hariavm

    hariavm

    Joined:
    Aug 18, 2014
    Posts:
    73
    are u got the desired output????
     
  5. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    Say I have 50 Triggers and 50 objects entering those Triggers
    I didn't want to have an "OnTriggerEnter" script on each of those Triggers, I just wanted one script that would check, what's entered what Trigger based on Tags

    do you think I could request to have what I was trying to do as a feature in the next version of Unity?

    anyway

    @aaro4130 thanks for the sendmessage examples

    Thanks for the help guys
     
  6. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    You could always have a very short script that you apply to all your triggers (if they're all the same, say from a prefab, you wouldn't even need to manually apply them) in which, when something enters, exits or stays in the trigger zone, the script just sends a message to a single trigger manager script, letting it know that this particular trigger was, well, triggered. The trigger manager script could be placed on an empty gameobject.

    Something like aaro's script would work, but instead of sending the message to the object involved with the trigger, send it to the manager script instead.

    As far as feature requests, I guess there's no harm in asking.
     
    graviton likes this.
  7. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75