OnTriggerEnter2D works but other.tag doesn't
I have a script that adds outlines to objects when the player collides with them. The OnTriggerEnter2D works but I want to make it so the Player is the only one who triggers the outlines, as soon as I add an "if" check with other.tag, wathever is inside doesn't work anymore. This is the code:
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
AddOutline();
}
}
The Player has the "Player" tag and the script containing that code is in the Objects the player collides with.
Now in another script it has a different effect.
void OnTriggerStay2D(Collider2D other)
{
if (other.tag == "Player")) {
if (this.gameObject == GameObject.FindGameObjectWithTag("Player").GetComponent<OutlineObjectsPlayer>().closestObj)
{
onTriggerEnter = true;
AddOutline();
}
else
{
onTriggerEnter = false;
RemoveOutline();
}
}
}
This is supposed to, every frame the player is in the object collider, check if the triggerer is a player, check if the object is the closest to the player, and then add the outline.
This time the code works but it doesn't care if the triggerer is the player, other collisions add the outline as well. I don't even know anymore.
Answer by Torqus · Sep 02, 2017 at 08:07 AM
Turns out the collider needs the "Is Trigger" check... EDIT: Nah the problem was I can't use OnCollider and OnTrigger if my layer collider matrix ignores both colliders. The solution was to add a "Triggerer" layer and a "Triggerer" box collider that collides with everything as a child of my Player. I find it weird that nobody talks about this in tutorials and stuff. Should I not be doing this?
Your answer
Follow this Question
Related Questions
Tagging Terrain Textures 0 Answers
Change Enemy Direction 1 Answer
Raycast2D Two Objects at the same time 2 Answers
How to check for tags? 1 Answer