- Home /
OnTriggerEnter2D other.gameobject.tag not working
so I have a code
void OnTriggerEnter2D(Collider2D other) {
Debug.Log("Happens");
testbool = true;
if (other.gameObject.tag == "EnemyCanHitMe")
{
LeftHitterBool = true;
}
else
{
LeftHitterBool = false;
}
}
It should make the LeftHitterBool true when the player enters the box collider 2D of an object with the tag EnemyCanHitMe but the bool isn't becoming true when the player enters the collider. Anyone know a solution to this? I've checked multiple times and the correct tag has definitely been assigned to the box colllider. EDIT: The OnTriggerEnter2D part itself is working, but it's the if statement that doesn't seem to function
Are you sure your collider is marked as IsTrigger? Does one of your objects have Rigidbody2D? Is your trigger event happening? Did you Debug.Log the tag of 'other' gameObject?
both of the objects have a rigidbody2D attached and are marked as IsTrigger. The trigger enter itself is being detected but the if statement doesn't work
Ok, so did you debug it? Is the tag really what you expect? And not in the inspector, in the code
Answer by LeonmFF · Oct 10, 2018 at 08:23 PM
First, try gameObject.CompareTag("EnemyCanHitMe"), Make sure that the collider is set to trigger \o/
Answer by nurihanozalp · Mar 22, 2021 at 03:38 PM
Did you put BoxCollider2D to the object tagged "EnemyCanHitMe"? If you used a BoxCollider (3d one) the OnTriggerEnter2D wont do its job due to the dimension problems
Your answer
Follow this Question
Related Questions
GameObject tag to if condition 1 Answer
Help With OnTriggerEnter (Collider) 1 Answer
OnTriggerEnter doesn't read a tag of a moving object 1 Answer
How to work with tags ? 1 Answer