resc73
1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExplodeCubes : MonoBehaviour
{
private bool _collisionSet;
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);
}
}
}
code just dont working
how it actually work https://i.stack.imgur.com/nNEho.png
how it need to be working https://i.stack.imgur.com/WoaaF.png
Is that first debug log working?
What is the parent?
transform.parent.gameObject;
Original code:
resc73
4
Yes, im used that code and its dont working…
Hi @resc73
In your first image I can see that, AllCubes is the parent gameobject and the other cubes are children.
In your code here
GameObject refToParent = transform.parent.gameObject;
you are trying to get the parent of AllCubes which is not possible because AllCubes don’t have a parent, it is the parent.
You need to correct it like this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExplodeCubes : MonoBehaviour
{
private bool _collisionSet;
private void OnCollisionEnter(Collision collision)
{
Debug.Log("Collison");
if (collision.gameObject.tag == "cube")
{
for (int i = transform.childCount - 1; i >= 0; i--)
{
Transform child = transform.GetChild(i);
child.gameObject.AddComponent<Rigidbody>();
child.gameObject.GetComponent<Rigidbody>().AddExplosionForce(70f, Vector3.up, 5f);
child.SetParent(null);
}
}
}
}
Make sure Platform is tagged as "cube"
And all child cubes have box colliders.
1 Like
resc73
6
im used this script on parent
resc73
7
very big thanks for help.