Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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. http://i.imgur.com/rLnOM.png

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
1  
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. unity3d.com/support/documentation/Components/… –  Tetrad Apr 30 '12 at 16:44

2 Answers 2

  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]unity3d.com/support/documentation/Components/… –  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  
unity3d.com/support/documentation/ScriptReference/… "OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider." (emphasis mine) –  Tetrad May 1 '12 at 17:30

Your Answer

 
discard

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.