Home > Software engineering >  OnTrigger2D doesn't seem to be called
OnTrigger2D doesn't seem to be called

Time:03-24

I am facing a problem with unity on a 2D project. OnTriggerEnter2D doesn't seem to be called as I have no prints on console.

Here is the script that calls the OnTriggerEnter2D

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AbilityUnlock : MonoBehaviour
{
    

    
     void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log("Test");
        if(other.tag == "Player")
        {
            Debug.Log("Player tag ");

            Destroy(gameObject);
        }
    }


}

gameObject Collider

The Collider of the object Is trigger is on!

Project Collisions

The Project Collisions

RigidBody2D of the Player

The rigidbody 2D of the player

Tag of the Player

The Tag of the Player and the Layer

The Script is attached to the gameObject.

Probably I am missing something that I can't see.Any help is very appreciated.

Thanks

CodePudding user response:

There are five things I always check when having trigger enter issues.

  1. Both objects are on physics layers that can interact with each other
  2. Both objects to have collider2d components
  3. The entered object has a rigidbody2d component
  4. The entered object's collider2d is set to trigger
  5. The entering object's collider2d is not set to trigger

From your question it look like you've already got steps 1) and 4) looking good.

As other answers point out the most likely candidate is 3) - rigidbody2d not being on your entered object.

If none of that works, then you can try some more extreme measures such as setting the rigidbodys to never sleep or playing with the interpolation mode, but give that a go.

CodePudding user response:

You need Rigidbody2d Component on gameObject. Not only on player.

  • Related