I’m just starting out with Unity and am following through a tutorial from Jeremy Gibson’s book. I have hit a problem though with onCollisionEnter not being called when two objects collide.
The objects are
1.) A cube (Basket) with a box collider
2.) A sphere (Apple) with a non-kinematic rigidbody and a sphere collider
When the apple hits the basket it should be deleted.
The following script is attached to the basket:
using UnityEngine;
using System.Collections;
public class Basket : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 mousePos2D = Input.mousePosition;
mousePos2D.z = -Camera.main.transform.position.z;
Vector3 mousePos3D = Camera.main.ScreenToWorldPoint (mousePos2D);
Vector3 pos = this.transform.position;
pos.x = mousePos3D.x;
this.transform.position = pos;
}
void onCollisionEnter(Collision coll) {
GameObject collidedWith = coll.gameObject;
if(collidedWith.tag == "Apple") {
Destroy(collidedWith);
}
}
}
I’ve had a long google around, and all the similar problems seem to be with not having a non-kinematic rigidbody involved.
There is a collision occurring when the game is run (as in the Apple hits the Basket and bounces off), but the onCollisionEnter method from the Basket script is not being called.
Any ideas what might be causing this?