TRIGGER DETECTION I bet you can answer..

I have googled my face off with no answer.

Seems pretty simple but cannot find a solution.

I have several trigger box colliders in my scene . I have the
“Is Trigger” checked.

I cant for the life of me test to see what trigger my player hit.

I can only get the Collider info but not the actual trigger info.

Seems so simple yet not one person has ever wanted to know?

I don’t want to write a separate script for each trigger,

I want to test to see what trigger I just hit so I can decide what value to

give a variable. I have been programming since the 1990’s and cannot for the life of me

figure out why this is not a common need.

Thanks in Advanced! :slight_smile:

Hi, in OnTriggerEnter you have acces to the whole gameObject from the collider parameter, up to you to use the object’s tag, name or whatever data transport design you’re trying to achieve !

For example :

public class ExampleClass : MonoBehaviour
{
    void OnTriggerEnter(Collider other)
    {
         if (other.gameObject.tag == "ACoolTag")
         {}    //Do cool stuff
         if (other.gameObject.name == "ThatAwsomeTriggerGO")
         {}    //Do even cooler stuff
    }
}

Fast Reply! very cool.
Ok I should explain better.

I have 10 models wearing a different dress each. They all have a trigger box around them.

If I trigger a box around a model with say a red dress I want it to spin when I enter and stop when I exit.

When I enter the trigger I also want to define what model to rotate. for example.

void OnTriggerEnter(Collider other)
{
if (trigger.name == “trigger 1”){ offon = 1;}
if (trigger.name == “trigger 2”) {offon = 2;}
if (trigger.name == “trigger 3”) {offon = 2;}
}

I just would like to test which trigger my player has entered so the update loop can spin the proper model. like this.

void Update () {
if (offon == 1) {
sp1.transform.Rotate (0, 6.0F * rotationsPerMinute * Time.deltaTime, 0);
}
if (offon == 2) {
sp2.transform.Rotate (0, 6.0F * rotationsPerMinute * Time.deltaTime, 0);
}
if (offon == 3) {
sp3.transform.Rotate (0, 6.0F * rotationsPerMinute * Time.deltaTime, 0);
}
}

and to stop the spin when I exit the trigger

void OnTriggerExit(Collider other)

{
offon = 0;
}
}

Seems so simple to me but I cannot find away to test what trigger is sending the enter message.

It seems the only solution is to have the same scritpt for each trigger and rename the script class and script filename

where if I was able to detect I can just use the same script for all 10… I am sure Unity3d must have their reason and assume the trigger with its own script should not share its script with other triggers…

Thanks

^^

First your should use the CODE [tag] so your post is easier to read !

Now, in this code:

void OnTriggerEnter(Collider other)
{
    if (trigger.name == "trigger 1"){ offon = 1;}
    if (trigger.name == "trigger 2") {offon = 2;}
    if (trigger.name == "trigger 3") {offon = 2;}
}

What is the “trigger” variable ? Because all you need to do is check “other.name” and you’ll have the name of the object you’re looking for, no need to clutter your code base with unused classes…

void OnTriggerEnter(Collider other) // <-------- this variable is the trigger your object has entered
{
    if (other.name == "trigger 1"){ offon = 1;}
    if (other.name == "trigger 2") {offon = 2;}
    if (other.name == "trigger 3") {offon = 3;} //carefull here you had an error with the number...
}

Hi Again. other.name is the object that hits the trigger …

(Collider other) /// this variable is the object that entered your trigger.

there is no way to test what trigger is hit because the
trigger event assumes it is the trigger that the script is attached to.

I had made a prefab of the trigger and the model and the script. as soon as I hit the first trigger to make the model spin then no matter what trigger I hit after that the first model would only spin. I just made 10 versions of the same script with different names pointing to the proper model … it works … I just am lazy . wouldn’t it be nice if ontriggerenter acted like an update function and to could just write 1 script file for all your triggers… but I am sure if your game has thousands of triggers this could get messy quick!

I really appreciate the time you have taken to help me out .

Thank!

I thought you had your script on the moving object, in which case the parameter would have been the trigger you’re entering.

Now I don’t understand how come you have that “offon” variable on the trigger and that it rotates the objects where else…

Anyways, good luck with your project sir :slight_smile:

The trigger is a component of a GameObject. You can name different gameObjects as you wish and in code just check for gameObject.name.