instantiating a prefab makes disables all components

I am trying to implement a respawn in my code. I have this Explode script that I attach to anything I want to die. It used to kill the game object, spawn some cool gibs and then reload the scene. Now I want instead of reloading the scene to check if what just exploded was the player? and if so respawn him at an xy. I thought I had it done slick:

    public void OnExplode(){
        if(explosionSound)
            AudioSource.PlayClipAtPoint(explosionSound, transform.position);
        //back up object to later check it out.
        GameObject spawned = gameObject;
        Destroy (gameObject);  //kill object

       //spawn gibs 
       //do other house cleaning
       //reset things...
        if (this.gameObject.tag == "Player") {
            Player pl = spawned.GetComponent<Player>();  //get player before it was destroyed
            spawned.transform.position =pl.respawnPoint; //get that respawn point
            GameObject clone = Instantiate(spawned) as GameObject;
            clone.transform.position = pl.respawnPoint;
            clone.name = "Astronaut";
        }   

    }

I think I am doing it the hard way, I am maybe curious if the GameObject i save before I destroy the gameObject is just the reference to the component or the whole prefab the Explode component is a part of?

I cannot figure out why the object spawns but everything in it is disabled (and in the prefab it is all enabled on disk)

Spawned is storing a local reference to the gameObject. When you call Destroy it disables all its components before removing them. You’re instantiating the reference with disabled components. You’ll want to do the player check and instantiation before calling Destroy.

3 Likes

Yep just realized this, I did more hunting and found this:

it fixed the issue awesome like.