Objects collide but OnCollisionEnter is not triggered

I have a box collider 2D for both my objects, they both have rigidbody set to dynamic without is trigger checked, and the layers are set to collide. they do collide, I see the objects interact, but the function OnCollisionEnter is not triggered (the debug.log isnt showing).
any suggestions to why is that?

Please be more precise when describing your setup. You said BoxCollider2D but you also said rigidbody, not rigidbody2d. Likewise you said OnCollisionEnter and not OnCollisionEnter2D. So currently you’re mixing terminology from two separate physics systems and they can not be mixed. So please make sure you either use 3d physics (Physics, Rigidbody, Collider, BoxCollider, OnCollisionEnter, …) or 2d physics (Physics2D, Rigidbody2D, Collider2D, BoxCollider2D, OnCollisionEnter2D, …)

Im using the 2D rigidbody and function.

Great, so we assume that you have created a MonoBehaviour script that you have attached to the object with the Rigidbody2D on it and this script implements the OnCollisionEnter2D method, right? You do have a Debug.Log statement inside that method as the first statement and when you collide with something, that log message does not appear in the console? You also do not get any errors in the console either, right? If that’s all the case, I have no idea. However if anything is not the way I described it, there are many different reasons why the callback isn’t called. Though we can’t determine that without more information.

The most common reason is that you’ve either spelled “OnCollisionEnter2D” incorrectly or that you’ve not used the correct case i.e. “OnCollisionEnter2d” etc. Neither of those things will give you compilation error.

Please post the scripts used.

I’m also curious because if it’s in any way related to the code you posted yesterday, that’s clearly using 3D physics: https://discussions.unity.com/t/931891

it is not the same project. this is the code that’s supposed to be executed.

void OnCollisionEnter2D(Collision2D col)
    {
        Debug.Log("OnCollisionEnter2D");
    }

It’s spelt correctly then but doesn’t really tell me much beyond that. I can only assume that it’s on one of the GO involved in the collision.

Well i don’t know what else to provide… the rest of the script works just fine.

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

public class ShooterController : MonoBehaviour
{
   public GameObject[] shooterPrefabs;
    private GameObject currentShooter;
    private Vector3 position = new Vector3(0,-10,0);
    public float shootSpeed = 5.0f;
    private Rigidbody2D shooterRb;

    void Start()
    {
        SpawnRandomShooter();

    }

    void Update()
    {
      
      
        if (Input.GetMouseButtonDown(0))
        {
            ShootBubble();
            SpawnRandomShooter();
        }
    }

    void SpawnRandomShooter()
    {
        int randomIndex = UnityEngine.Random.Range(0, shooterPrefabs.Length);
        currentShooter = Instantiate(shooterPrefabs[randomIndex], position, Quaternion.identity);
    }

    void ShootBubble()
    {
        Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        mousePosition.z = 0;
        Vector3 direction = mousePosition - position;
;       float shootForce = 5.0f;
      
        shooterRb = currentShooter.GetComponent<Rigidbody2D>();
        shooterRb.velocity = direction * shootForce;
        //shooterRb.AddForce(direction*shootForce,ForceMode2D.Impulse);
        Debug.Log(direction);
        Debug.Log(transform.position);
      
        //currentShooter.transform.parent = transform;
    }
  
   void OnCollisionEnter2D(Collision2D col)
    {
        Debug.Log("OnCollisionEnter2D");
    }
}

Maybe zip it up and send it to me and I’ll tell you why it’s not working? Here or DM me?

Does the object that has the “ShooterController” script attached actually have a rigidbody attached as well and is moving? Or do you expect that this “shooterRb” (which is the rigidbody2d of a completely different instantiated prefab) somehow triggers the collision message on a completely different object? This message is only send to objects that are actually involved in the collision. So that’s the object that has the rigidbody2d attached or the other collider2d / rigidbody2d that was hit. Your code doesn’t look like you actually move the object where this ShooterController is attached to.

MonoBehaviour scripts are “behaviour” scripts. So you have to attach them to objects to give them a certain behaviour.

Agreed.

I asked this and above, the callback is absolutely NOT on the things being shot by the look of it, it’s on the “controller”.

I don’t fully understand…
the shooter controlloer is atttached to a shooter empty that spawns the prefabs they are moving towards another object and colliding, but it’s just the function that isn’t triggered. both have rigidbody2d.

But the OnCollisionEnter2D callback is CAUSED by a rigidbody when it detects a collision. You have to have a script ON the object that has the rigidbody in order to get this message. OnCollisionEnter2D is not called on all objects in your scene. Your shooter object has nothing to do with the collisions in question. Those are between your instantiated prefab(s) and some other object(s). If you want to detect collisions of those objects, you have to have a script on those objects which implement the OnCollisionEnter2D method.