- Home /
Unity collision detection not working.
So I have 2 prefabs, 1 with a 2d box collider, and one with a 2d box collider and a 2d Rigidbody:
I instantiated both of them, the robot prefab in a robot script and the grass prefab in the board manager script. I want to check if the robot is colliding with the grass in the code, so I add this in the robot script:
void OnCollisionEnter2D(Collision2D coll) {
Debug.Log ("Detected!");
}
When I run, the robot falls onto the grass, and the console remains silent. Did I do something wrong?
Where do you exactly have that code? Your screenshots don't show any script in any of the gameobjects. That code has to be in a component of one the gameobjects to work.
In your screenshot, you have not attached any script to Robot_prefab or grass !!!!
are you sure you have attached rigidbody to the robot?
Not sure why it's not working. You might want to enable isTrigger and use OnTriggerEnter. Unity's documentation/example is profoundly uninformative for this though: Collider2D.isTrigger documentation
Answer by Cynikal · Aug 30, 2016 at 06:19 AM
I reread your question.
If the console remains empty, do you have the console box enabled to read logs?
I dont see the script with your code on either of the gameobjects you have in your screenshot.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how to prevent an gameobject of instantiating if theres an collider where it is supposed to collide 1 Answer
Layers of collision, or multiple collisions 1 Answer
OnCollisionEnter2D is not working when an object tagged "Enemy" is not present. 2 Answers