Unity Stop Destroy(gameObject) destroyinng objects instantiated by this script
I have a script to manage the death of a the player (attached to player) which instantiates fractured version of the player's model and apply's explosionforce to it then destroys itself. However when it does destroy itself it also destroys the fractured vesion does anyone know how to prevent this?
My code is as follows:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Death : MonoBehaviour { [SerializeField] [Tooltip("Prefab containing effect that is applied (Instantiated) on death")] private GameObject _deathEffectPrefab; [SerializeField] [Tooltip("UI Object to be enabled on death")] private GameObject _gameOverUI; [SerializeField] [Tooltip("How long to wait (In Seconds) for death effect to complete before showing death UI")] private float _uiEnableWait; [SerializeField] private GameObject Camera; public float _explosionforce; public float _explosionRadius; private GameObject Player; private GameObject respawnManager; // Start is called before the first frame update void Start() { respawnManager = GameObject.FindGameObjectWithTag("Respawn"); Player = GameObject.FindGameObjectWithTag("Player"); } // Update is called once per frame void Update() { } void DestroyMe() { _gameOverUI.SetActive(true); Destroy(Player); } public void Die() { Camera.GetComponent<CameraFollow>().enabled = false; Debug.Log("Dieded"); GetComponent<Movement>().enabled = false; GetComponent<Jump>().enabled = false; GetComponent<MeshRenderer>().enabled = false; GetComponent<BoxCollider>().enabled = false; GetComponent<Rigidbody>().velocity = Vector3.zero; GameObject Effect = Instantiate(_deathEffectPrefab, transform); Collider[] debrisColliders = Physics.OverlapSphere(transform.position, 0.5f); foreach (Collider NearbyDebris in debrisColliders) { Rigidbody debrisRB = NearbyDebris.GetComponent<Rigidbody>(); if (debrisRB != null) { debrisRB.AddExplosionForce(_explosionforce, transform.position, _explosionRadius); } } Invoke("DestroyMe", _uiEnableWait); } }