- Home /
Trigger not working with a specific game object
Hello everyone, I have a problem with a trigger. Basicaly I want the the trigger to work only when the player pass on it. But if an enemy collide with it or a bullet collide with it, it gets activated. I tried to put a tag on the player but it doesn't seems to work because the trigger doesn't recognize the player or the bullet shooted from his gun. Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StartTrigger : MonoBehaviour
{
public GameObject Player;
public GameObject Spawns;
public GameObject Trigger;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
Spawns.SetActive(true);
}
}
void OnTriggerExit2D(Collider2D other)
{
Trigger.SetActive(false);
}
Usually , when I face issues with triggers not detecting the proper gameobject , it was always my fault . Sometimes I add the tag and forget to assign the tag to the Gameobject . Sometimes the Trigger and Tag are in different parts of the hierarchy.
I had already assigned the tag to the player. Now I've tried to assign the same tag to the trigger but it dosn't work anyway. And yes, right now the Trigger and the Player are in different part of the hierarchy. Do you think it's a problem?
Answer by Chimer0s · Jan 23 at 08:22 AM
The tag should definitely work. Is "Trigger" the gameObject with this script on it? Because currently anything that exits this trigger will cause that to be turned off. If you want to make sure that nothing besides the player will interact with this collider, the most foolproof way is to place the collider on a layer that doesn't interact with any layers besides the one the player is on. That way nothing else will be able to trigger collisions with it. For example, you create a layer called "Player" and another one called "Player Detection." Then you place the player on the Player layer and this trigger on the Player Detection layer. In your physics settings, you go to the collision matrix and uncheck all the other layers from the list of those that can interact with player detection so only the box for the player layer is checked.
Yes the Trigger is "tirgger". I've created a layer for the Trigger (the player has already a layer called "player") and deleted the part of the code with compare tag but it doesn't work because the bullet of the player's pistol interact with the trigger even if it is on a different layer. I've also unchecked all the other layers apart from the player layer.
[1]: /storage/temp/191584-immagine-2022-01-23-095546.png
Do I have to also write someting in the code?
There has to be something configured incorrectly then. It's not possible for a layer that isn't the Player to interact with the InteractableTrigger layer if you have it configured the way it is in that image. Either the bullet is also on the Player layer(or maybe has a child collider on that layer) or the trigger isn't on the InteractableTrigger layer. The only other possibility I could think of is if your bullet is using a raycast and manually calling ontriggerenter on anything it hits instead of it actually being a collider being detected, but I'm sure you would have mentioned that if it were the case. If everything is 100% configured correctly, try restarting Unity. It's caused me some headaches over the years when I was sure I had everything right and it wasn't working. I'd waste 2 hours trying to trouble shoot the bug then just restart Unity and it would work as expected.
I'm dumb, I've changed the layer on the wrong trigger and obiusly it doen't work. Now I've the doubt that maybe is the same thing for the Tag. Thank you very mutch for your help.