OnCollisionEnter not functioning like I understand it.

I am currently generating my world at runtime and I am trying to do some error-checking to make sure sure pieces don’t overlap each other.

As I place pieces ion my world, I am dynamically generating the BoxCollider to automatically fit the placed object and that part is working great. I also set this BoxCollider as a trigger as well.

I then attach a RigidBody component to the same object, setting it to isKinematic and ignoring gravity. Then I attach a simple OnCollisionEnter script that should just log out a statement if it even hits anything.

But no matter how bad the objects overlap, nothing is getting registered.

public class DungeonCollision : MonoBehaviour {
	void OnCollisionEnter(Collision collision) {		
		Debug.Log("We hit something");		
	}
}

I add my RigidBody with the following:

	void AddRigidBody(Module newModule) {
		var rigidbody = newModule.gameObject.AddComponent<Rigidbody>();
		rigidbody.isKinematic = true;
		rigidbody.useGravity = false;		
	}

As I create a new object, I simply call functions like so:

SetBoxCollider(newModule);
AddRigidBody(newModule);
newModule.gameObject.AddComponent<DungeonCollision>();

These are all being called withing a larger GenerateWorld() function that is called in Start();

Perhaps there is a better way to do this? I was attempting to use the OnCollisionEnter however, that would register a collision with a large container object but never with anything placed in it as well.

…I also set this BoxCollider as a trigger as well…

That’s where you’re going wrong. If your collider is a trigger, it will fire the OnTriggerEnter callback, not the OnCollisionEnter callback.

You’ve probably just confused OnCollisionEnter with OnTriggerEnter

When a collider is marked as isTrigger, you need to use OnTriggerEnter , OnTriggerExit and OnTriggerStay to detect its collision.

Perhaps a question to ask along side with my thought process of how it works, is when is OnCollisionEnter called? Or for that matter, OnTriggerEnter? This is all happening during my world generation within the Start() function. Should I be expecting these to be actually executed?

I would imagine that I should still see some notifications after the world generation is finished and the scene is running with the objects overlapping.