Destroy and then Respawn player

Hi Guys,
I’m very new to programming. But I have spent hours trying to do one thing and I can’t work it, so I’m asking for help.

What I am trying to do is destroy my Player and then after a few seconds respawn it back in but I am also doing this in a script that isn’t the Player script. I am trying to do this with coroutine and the IEnumrator. I know how to use the coroutines as I have already set something else up.
Here is how the code is setup that I want to do this in.

void OnCollisionEnter2D(Collision2D collision){
        ScoreManager.score +=ScoreValue;
        StartCoroutine(Respawn());
    }
   
    IEnumerator Respawn(){
        //Destroy the character
       
        yield return new WaitForSeconds(3);
       
        //respawn
    }
}

Any help would be much appreciated

Why not just deactivate and activate the object or renderer component?

I feel like your best course of action here is to set Enabled = False for the player object.

The first step would be to retrieve the players GameObject.
Try using GameObject.FindWithTag() and place a “Player” tag on the player object.
See the link for more information on the method: FindWithTag()

Once you have the game object you can find the script Component
It would look something like var mb = GameObject.GetComponent();

After that you would invoke the method
mb.Invoke(“Respawn”, 3);

More on Monobehaviour.Invoke()

Privacy & Terms