- Home /
How do you find the tag of the object that is a trigger?
So, I'm new to unity, so I don't know if this is possible, but: How do you check the tag of the object that is in a trigger? Any way to do this would work.
For example, I have a two player game with a selection screen that uses two cursors to choose, and I want the buttons to be able to discern the tag (Player1 and Player2) so that my game knows which character is chosen by which player. On a side note, the cursors are rigidbodies, and the "button" is a trigger. This is because it is local multiplayer, meant to be played on a single keyboard.
Answer by Feelnside · Jun 19, 2018 at 04:48 AM
You don't need to use the tags there.
Once a Player has clicked the Player1 button, you need to assign to him that you are the Player1.
For example you can use int playerID (0 - not selected, 1 - Player1, 2 - Player2) at each Player.
Once the Player clicks the Player1 button, we need to ask another Player "what is your playerID?". If ID is not equal to the selected button, we can assign it to the Player like playerID = 1;
Oh. Sorry, I forgot to mention this was local multiplayer. Unless i'm interpreting your answer incorrectly?
Are you using rigidbodies (cursors) and trigger (button)?
In case if yes, you can add to the first cursor the "Player1" tag and to the second one the "Player2" tag.
And then just add a script into the trigger (button)
void OnTriggerEnter(Collider col)
{
if(col.CompareTag("Player1"))
{
//this is the 1st Player
}
else if(col.CompareTag("Player2"))
{
//this is the 2nd Player
}
}
It seems for me that OnTriggerEnter isn't working at all. I have a print command located in OnTriggerEnter and it doesn't seem to be working. $$anonymous$$y button is a trigger and both cursors are rigidbody2d, but the console doesn't print anything when they collide.
Your answer
Follow this Question
Related Questions
Detecting collisions 3 Answers
Game wont restart in the build, but works great in the editor.. 1 Answer
colliding with child trigger? 0 Answers
Destroying Multiple Objects with Tags 1 Answer
Set a Tag Multiple GameObject[] 1 Answer