Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using a script to instantiate multiple objects having rigidbody with different velocities at different positions. I want to destroy two or more objects when they collide, but I'm not getting anything inside:

void OnCollisionEnter(Collision collision){}

The details of the object I'm instantiating.

The script is attached to an empty GameObject and Instantiated GameObject is stored in a array of GameObjects:

 Block[0] = Instantiate(boxPiece) as GameObject;

where boxPiece is the Object I want to instantiate. And I'm checking for collisions in this very same script.

share|improve this question
Not enough detail here to really answer your question. I'd suggest going through the documentation again, and possibly providing screenshots of the inspector window of the objects you're trying to collide with each other.… – Tetrad Apr 30 '12 at 16:44
  1. Check that the instantiated objects have a collider.
  2. Are your objects moving at very high speeds? If they are they might jump over each other when they move faster than the length of the opposing collider.
share|improve this answer
1. Yes they have Colliders. 2. No. – 2600th Apr 30 '12 at 12:50
Hmm, could it be the collision layers then: [link]… – Esa Apr 30 '12 at 12:58
No everything is in default layer and every layer is checked in Physics. – 2600th May 1 '12 at 4:49
up vote 0 down vote accepted

After reading this Unity collision question I created a new script with void OnCollisionEnter(Collision collision){} and attached it to all the new Objects I'm creating in my script using AddComponent("NewScriptName") now it does detect collisions but I still feel that it's not the right way to do it.

share|improve this answer
That is the right way to do it. OnCollisionEnter will only get called on the object that is doing the colliding. Where was this OnCollisionEnter callback before? – Tetrad May 1 '12 at 15:39
It was inside the script which was instantiating these items using the code i mentioned in the question. – 2600th May 1 '12 at 16:51
1… "OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider." (emphasis mine) – Tetrad May 1 '12 at 17:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.