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