using System.Linq; using UnityEngine; using System.Collections; [SelectionBase] public class BreakableObject : MonoBehaviour { public Health health; public RandomSoundPlayer player; public GameObject[] instantinateOnDestroy; // Use this for initialization void Start () { if (health != null) { health.Destroyed += health_Destroyed; } } void health_Destroyed() { Break(); } public void Break() { var thisRb = GetComponent(); var children = gameObject.GetComponentsInChildren(); foreach (var child in children) { var rb = child.gameObject.AddComponent(); rb.mass = thisRb.mass/children.Count(); child.transform.parent = null; } if (player != null) { player.Play(true); } if (instantinateOnDestroy != null && instantinateOnDestroy.Length > 0) { foreach (var o in instantinateOnDestroy) { var item = Instantiate(o); item.SetActive(true); item.transform.parent = null; } } Destroy(gameObject); } // Update is called once per frame void Update () { } }