What do I do to improve issue in my code? Unity3D C#

Hello! I wrote a script that have to un disconnect cubes from each other when touches the floor but script isn’t working. (images below)

How it need to be working:

How it is working:

Here is the code:

using UnityEngine;

public class ExplodeCubes : MonoBehaviour
{
    private bool _collisionSet;

    private void OnCollisionEnter (Collision collision)
    {

        if(collision.gameObject.tag == "cube" && !_collisionSet)
        {
            for (int i = collision.transform.childCount - 1; i >= 0; i--)
            {
                Transform child = collision.transform.GetChild(i);
                child.gameObject.AddComponent<Rigidbody>();
                child.gameObject.GetComponent<Rigidbody>().AddExplosionForce(70f, Vector3.up, 5f);
                child.SetParent(null);
            }
            Destroy(collision.gameObject);
            _collisionSet = true;
        }
    }
}

Why it is not working? Help me please :frowning:

i’m not sure to anderstand what you want to do . But if you want to create an sort of explosion when a child cube hit something , you need to get the reference of the parent
Try something like that.

private void OnCollisionEnter(Collision collision)
    {
        Debug.Log("Collison");
        if (collision.gameObject.tag == "cube")
        {
            GameObject refToParent = transform.parent.gameObject;
            for (int i = refToParent.transform.childCount - 1; i >= 0; i--)
            {
                Transform child = refToParent.transform.GetChild(i);
                child.gameObject.AddComponent<Rigidbody>();
                child.gameObject.GetComponent<Rigidbody>().AddExplosionForce(70f, Vector3.up, 5f);
                child.SetParent(null);
            }
            //Destroy(collision.gameObject);
        }
    }

You have to give this script to each children cube with a rogibody on it and a collider