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
}
}
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…
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 …
void OnTriggerEnter(Collider other) // <-------- this variable is the trigger your object has entered
(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 .