Top-down 2d game ontriggerenter enter and exit

I can’t get my code to work I have added collision set as trigger to my enemy with a rigidbody2d and a my victim has collider and rigid body.

There are on the same layer my debug log won’t even work.

In the end my goal is to get my enemy to follow player when player (or victim) enters trigger and when he leaves I’ll try and put a cool down and enemy goes back to patrol idle. Screenshot of my debug down below. If you can at least point me in the right direction why my debug test will not work that will help too.

using UnityEngine;

using System.Collections.Generic;

public class Enemy : MonoBehaviour {

public float speed;
    public Transform victim;
	
    //private NavMeshAgent navComponent;

	

    // Use this for initialization

	
	
    // Update is called once per frame


	

    void OnCollisionEnter (Collider2D Collision2D){
		
        Debug.Log("something is here");
		
        if(Collision2D == victim){
			
            Debug.Log("Player is inside");
			
            //this.transform.Translate(victim.position * speed);
		
         }

	
    }
	

    void OnCollisionExit (Collider2D other) {
		
       Debug.Log("its working");

		
       if(other == victim){
			
           Debug.Log("its working");
		
      }   

}
}

In your collision Enter method you have this:

 if(Collision2D == victim){

‘victim’ is defined earlier in your class as a transform so what you’re saying here is “if the Collision object is equal to the victim transform object”. This will never be true as a Collision2D object isn’t the same as a transform, so you’re comparing two completly different things. I guess what you want is

if(Collision2D.gameObject.transform == victim){

The same thing is true in your collision exit routine. You’re comparing ‘other’ to ‘victim’ and again, the two will never be equal.

Hi. Since you’re working in 2D you want to use OnCollisionEnter2D. The absence of the “2D” bit in your method is part of your problem. Secondly, since your using trigger colliders you want to be using OnTriggerEnter2D. This will activate in collisions that involve trigger colliders, while the other is used if both are not. For clarity, if the collision involves a trigger collider use OnTriggerEnter2D (andExit2D, etc.) on both, regardless of which one is the actual trigger.

Not sure if this is the problem but in line 13

void OnCollisionEnter (Collider2D Collision2D)

You set the Collider2D variable name to Collision2D which is also a data type.

Also, the data type for OnCollision(Enter, Exit, Stay) (2d or normal) is Collision or Collision2D respectively, as Collider and Collider2D are for OnTrigger(Enter, Exit, Stay) (2D or normal.) Besides these things, try making sure that any collision is being recorded, not just that with the specified thing, and go from there.

void OnCollisionEnter2D(Collision2D coll) replace : 13
if(col.transform == victim) replace : 15
void OnCollisionExit2D(Collision2D coll) replace : 21
if(col.transform == victim) replace : 23

replace 4 line and check it

Have you tried using the “OnCollisionEnter2D()” function instead? By extension, the same would apply to “OnCollisionExit2D()”.