Hi, I’m trying to use OnCollisionEnter2D, only the function is never called, although the objects have all the boxCollider2D and the rigidbody2D, I have already deactivated the kinematic type. What else am I wrong? I trust in your help
I see you spelling it multiple ways in your post. You may not realize this but there is only ONE correct way.
Review the docs for that method online to make sure you’re spelling it properly.
You also need to verify you are meeting 100% of the requirements to have this method called. They’re also listed in the docs.
If you got this code from somewhere, tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:
How to do tutorials properly:
Tutorials are a GREAT idea. Tutorials should be used this way:
Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don’t make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.
If you get any errors, learn how to read the error code and fix it. Google is your friend here. Do NOT continue until you fix the error. The error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.
Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.
Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.
Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.
Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
Either this, or double check your Components.
- It has to be Rigidbody2D. With the 2D, Rigidbody doesn’t work.
- Double check if all colliding objects are non-triggers. If at least one is a trigger it’ll call OnTriggerEnter2D instead.
- Double check if all colliding objects are dynamic rigidbodies.
- Check the layers the objects are on and see if they can even collide in the physics settings.
May be a dumb thing to ask, but have you linked your script to the objects?
i entered rigidbody2d, not rigidbody.
the Istrigger checkbox is disabled, and for safety I have declared the function ontriggerenter2d but it is never called.
all objects are dynamic and non-kinematic.
I looked at the position of the objects and being in 2D they are all at z = 0.
yes all the objects I need I have inserted. only that in the documentation I did not understand well, if oncollisionenter2d detects all types of collisions or only those of the objects within the script
Share your code with code tags.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShoot : MonoBehaviour
{
[SerializeField] private Camera mainCamera; //serve perche senno la posizione del mouse non viene presa dal centro dello schermo, ma dal l'angolo in basso a sinistra
public GameObject player;
public Rigidbody2D ammo;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector2 shoot = mainCamera.ScreenToWorldPoint(Input.mousePosition); //in questo modo l'input del mouse prende le coordinate dal centro dello schermo
shoot.x = shoot.x - transform.position.x;
shoot.y = shoot.y - transform.position.y;
shoot = shoot.normalized;
if (Input.GetMouseButtonDown(0))
{
Rigidbody2D bullet = Instantiate(ammo, player.transform.position, Quaternion.identity); ;
Physics2D.IgnoreCollision(GetComponent<Collider2D>(), bullet.GetComponent<Collider2D>());
bullet.velocity = new Vector2(shoot.x * 3, shoot.y * 3);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log(collision);
}
}

So this script does the shooting and has the player collision on it. If this script is on the player then the player needs a Rigidbody2D and a BoxCollider2D (or some kind of 2D collider). If the collider and rigidbody are on a child or parent object and not on the same object as this script, you will run into issues.
There are some other errors here you should fix:
First off, your instantiation looks funky. You are instantiating a Rigidbody2D and not a gameobject which i’ve never seen before. I don’t know why you are using IgnoreCollision either as there are better ways to ignore collisions than thru code.
Second, you shouldnt even have the collision function on this script. This is your shooting script and the only function(s) it should do is shoot or something related. You should create a new script called PlayerCollisionHandler or something and put all your OnCollisionEnter2Ds and OnTriggerEnter2Ds on it. This is just following clean code principles and should help you in the long run.
Lastly, I think it is good practice to check what your collider is actually hitting. Most of the time you want it to only hit certain things or when it hits a certain object (like projectiles hitting enemies) to call another function. So you can add an if statement under OnCollisionEnter2D and say:
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "InsertTagStringHere")
{
Debug.Log(collision);
}
}
And one other point, make sure when you are testing the game that the objects you are testing with are actually colliding via the Unity system. If you move the player does he bump into the ground, walls, etc. Or does he go through some of them? That will make sure that you have the right settings on the Rigidbody2D and Collider2Ds. If they go through objects, then something is set as trigger and the OnCollisionEnter2D wont work