- Home /
How to undo destroying a component, after 5 seconds.
Hi, I am trying to make a script so if the player's health = 0, then he dies.
So far I have this:
void KillPlayer(){
if(Health <= 0)
{
Health = 0;
Destroy(GetComponent<MeshRenderer>(), 2f);
Destroy(GetComponent<BoxCollider>(), 2f);
Destroy(GetComponent<PlayerMovement>());
}
}
And all this does, is makes it so nothing can collide with it, and it isn't visible, and can't move. How can I make the player "Respawn". I basically want to recreate the components I destroyed, and then set his position to 0, but I think that would be with a Vector 3 thing, ik how to do that part. I have no clue how to recreate components. (I want to delay the reacreation of the components by 5 seconds) If you need more info, let me know. Thanks in advance!
If you only want to get rid of a component temporarily then disable it rather than destroy it. You can disable the renderer, collider, and player movement script. Wait for 5 seconds, the re enable them.
renderer.enabled = false;
etc...
yield WaitForSeconds(5);
You can't use yield statements everywhere(Not in Update() for example), look them up in the docs for more info
How do I make it wait five second re enabling them, and how do I make it wait 2 seconds before it disables the renderer and collider?
Okay, so would I do
yield WaitForSeconds(2);
renderer.enabled = false;
yield WaitforSeconds(2);
collider.enabled = false
ScriptNameWithReference.enabled = false
yield WaitForSeconds(5);
//Set position code
renderer.enabled = true
collider.enabled = true
ScriptNameWithReference.enabled = true
?
Yes something very like that in a co-routine, which will accept yield statements. :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Finding the player that walked into a ray? 1 Answer
Third person movement similar to Max Payne 1 Answer
Loading a new level from a previous one. 1 Answer
Respawn Script Problem 1 Answer