1
\$\begingroup\$

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. https://i.sstatic.net/dKhgm.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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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/… \$\endgroup\$
    – Tetrad
    Commented Apr 30, 2012 at 16:44

2 Answers 2

2
\$\begingroup\$
  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.
\$\endgroup\$
3
  • \$\begingroup\$ 1. Yes they have Colliders. 2. No. \$\endgroup\$
    – 2600th
    Commented Apr 30, 2012 at 12:50
  • \$\begingroup\$ Hmm, could it be the collision layers then: [link]unity3d.com/support/documentation/Components/… \$\endgroup\$
    – Esa
    Commented Apr 30, 2012 at 12:58
  • \$\begingroup\$ No everything is in default layer and every layer is checked in Physics. \$\endgroup\$
    – 2600th
    Commented May 1, 2012 at 4:49
0
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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? \$\endgroup\$
    – Tetrad
    Commented May 1, 2012 at 15:39
  • \$\begingroup\$ It was inside the script which was instantiating these items using the code i mentioned in the question. \$\endgroup\$
    – 2600th
    Commented May 1, 2012 at 16:51
  • 1
    \$\begingroup\$ unity3d.com/support/documentation/ScriptReference/… "OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider." (emphasis mine) \$\endgroup\$
    – Tetrad
    Commented May 1, 2012 at 17:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .