Sunday, August 18, 2019

collision detection - Unity OnCollision() not working


Seem to have exhausted google looking for an answer to a VERY bullet simple code that should work.


void onCollisionEnter(Collision col) {
print ("hit " + col.gameObject.name);
Destroy (gameObject);
}


Bullet object has the above script, SphereCollider (radius a lot bigger than object), RigidBody, not a trigger, not kinematic, is travelling slow. All checked during runtime.


When it hits a wall (Box collider), it just bounces off but no collisionEnter code is ran.


Debug console bubble is on.



Answer



While you are trying to call onCollisionEnter(), MonoBehaviour uses the function OnCollisionEnter().


The difference is quite enormous, on the technical side of things. Being a function, the first letter should be a capital. By being lower case, your creating an entirely different function, that does not overload the original collision function your trying to call from MonoBehaviour.


To clarify, we are not overloading a method from MonoBehaviour, in the traditional sense. You should not use the override keyword, as this will report "no suitable method found to override".


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...