Destructible object multiple prefabs

Hi there :smiley:

I want to have a destructible object with multiple stages.
135837-untitled-1.jpg
I have 3 prefabs, first is clean, a second broken but still assembled, and a third shattered in pieces with rigidbodies.

Edit :
Prefab 1 and 2 are one GameObject with one Mesh Renderer and Box Colliders.

Only the 3rd prefab is a GameObject with multiple childs.

I have this simple script to begin with :

public class Destructible : MonoBehaviour
{
    public GameObject destroyedVersion01;

    void OnCollisionEnter(Collision collision)
    {
        if (collision.relativeVelocity.magnitude > 2)
            Instantiate(destroyedVersion01, transform.position, transform.rotation);
        Destroy(gameObject);
    }
}

If I attach the script to prefab n°1, everything works fine, prefab n°2 is instancied.

How can I manage to have the third prefab being instancied ?

If i attach the same script to the prefab n°2, the object just disappear when collision happend.

In hope I explain myself clearly.

from the screenshot I presume you have a basic cube model (or your alternative) with a simple box collider directly on that model, while with the toher two, it’s a GameObject with the Destructible script on it and children GameObjects with meshes. That’s the reason I can think of of this not working out for you from the screen provided.

If that’s the case, you need to attach the Destructible script on the gameobject that actualy has the mesh component, otherwise the collision won’t take place. (the Destructible only takes into consideration collisions on its own object and its mesh, not parents nor children.

If that’s not the case, please provide more details :slight_smile:

Ok I found it. It was a coding noob problem :

void OnCollisionEnter(Collision collision)     
{         
    if (collision.relativeVelocity.magnitude > 2)
    {
        Instantiate(destroyedVersion01, transform.position, transform.rotation);                     
        Destroy(gameObject);
    }
}

The if statement only included Instatiating the prefab. So the Destroy occured whenever a collision happened…