- Home /
2D collision won't work?
Ok so i am using a spawner which makes copies of a prefab. The clones fall to the ground and they should die, but they don't. The ground has a rigidbody2d and a box collider2d and so does my ball. Here is some of my code. public class DestoryPlayerScript : MonoBehaviour {
void OnCollisionEnter2d (Collider2D other)
{
Destroy (this.gameObject);
}
}
public class SpawnBallOnKeyPress : MonoBehaviour {
public GameObject Ball;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
Instantiate (Ball, transform.position, Quaternion.identity);
}
}
}
Answer by CheetahSpeedLion · Jul 17, 2017 at 01:24 AM
Does your prefab that you're spawning have a rigidbody? According to the Unity collision matrix that would be necessary for the collision to be registered: https://docs.unity3d.com/Manual/CollidersOverview.html
Areleli
Answer by tanoshimi · Jul 17, 2017 at 06:27 AM
Case-sensitivity matters. The callback is called OnCollisionEnter2D not OnCollisionEnter2d.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html
Your answer
Follow this Question
Related Questions
Gameobject not detecting collison from other Box Collider 2D [SOLVED?] 2 Answers
Problem with method Collider2D.isTouchingLayers() 4 Answers
Is it possible to detect collisions between 2d and 3d objects? 2 Answers
How to set collision for an object with specific collider size? 3 Answers
(Virtual reality) Collision fighting when tunneling - Any ideas? (gif included) 0 Answers