- Home /
Trigger that reacts to different tags? C#
Hello once again Unity Answers.
I've got a couple of triggers, lets call them 1, 2 and 3 and two gameObjects to interact with them called A and B. I want each trigger to do something different if an object enters it so I've written this that is doing the job. The number of course is checking which trigger it is with "gameObject.name =="
void OnTriggerEnter(Collider other)
{
//Trigger name determines which thing it'll do if something collides with it.
if(gameObject.name == "1")
{
//Do Stuff
}
if(gameObject.name == "2")
{
//Do Different Stuff
}
if(gameObject.name == "3")
{
//Do Even More Different Stuff
}
}
That works but now I want it too tell the difference between Objects A and B however I can't work out how to reference them (despite knowing how the trigger can check it's own name). A and B have tags on them of the same name so in pseudo code I expect it'll look something like this...
void OnTriggerEnter(Collider other)
{
if(Collider.tag == "A")// If what has just hit the trigger is "A"..
{
//..check Trigger Name and do the thing it's been assigned with
if(gameObject.name == "1")
{
//Do Stuff
}
if(gameObject.name == "2")
{
//Do Different Stuff
}
if(gameObject.name == "3")
{
//Do Even More Different Stuff
}
}
if(Collider.tag == "B")// If what has just hit the trigger is "B"..
{
//Do EVEN MORE different stuff again! //..do a different set of instructions
}
}
I'm only guessing it'll be something like "Collider.tag =="...unless it needs to be reference in the method at the top like..
void OnTriggerEnter(Collider gameObject.name == "A")
{
A Stuff
}
void OnTriggerEnter(Collider gameObject.name == "B")
{
B Stuff
}
...anyway I'm making this overly complicated, in a nutshell how do I make a Trigger know the difference between gameObjects/Prefabs using different tags?
Thanks for reading!
Answer by Joyrider · Aug 20, 2013 at 12:08 PM
You basically have what you need... I don't get why you are asking a question..
You should just use other.tag (other being the variable containing your collided object)
Thanks I just stumbled across a youtube comment that made it clear and you're right, it's "if(other.tag == "A")
I still have issues understanding exactly what void OnTriggerEnter("This bit") means.
void means this method does not return any value
OnTriggerEnter is obviously the name of your $$anonymous$$ethod
(Collider other) means, your method has an input variable called other of class Collider.
Now, OnTriggerEnter is called automaticaly by Unity. But if it were any other function and you had to call it. It means you would have to give the function the collided object as input variable.
OnTriggerEnter() would generate an error telling you, the mathod takes more than 0 parameters OnTriggerEnter(theObjectImColliding) would be the correct function call, and you would thus find theObjectImColliding in the "other" variable.
Your answer
![](https://koobas.hobune.stream/wayback/20220613112611im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
VR : How to check if object held passed through trigger Zone 1 Answer
MissingMethodException: 'UnityEngine.AudioClip.Play' 2 Answers
Trigger enter in single direction ??? 3 Answers
Can't click gameobject when over another trigger? 1 Answer
I cant change tag on two objects simultaneously? Why? 1 Answer