currently, i have this script attached to asteroid enemys, if they make contact with the player, it instantiated the player explosion and destroys both the asteroid and player objects, as well as making GameOver = true, ok. so what i want to do? I have another script which holds tokens available, what i need is that when this happens, the player is given 10 seconds and can choose to use a token to get revived and resume from exactly the same place the player died, the problem is that, how to uninstantiate the player and resume it ? if there’s no enough tokens then set GameOver to true.
sorry if my english is not very understandable , i try my best
You should have a GameManager object which handles this sort of thing. When the player explodes, don’t actually Destroy the player object. Instead just call SetActive(false) on it.
Then, GameManager can show the 10 second count-down. If the player chooses to spend a token, then it stops the countdown, and restores the player object by calling SetActive(true) on it.
public void GameOver ()
{
Gameoverpanel.SetActive (true); //if the game is over, shows the game over panel
DisableBoundary.SetActive (false); //disables boundary after player death so it stops counting score
increasetime = false;
if (PlayerPrefs.GetInt ("Currency", 0) > 0) {
Buttton.SetActive (true);
} else {
gameOver = true;
}
}
public void Button_Click ()
{
PlayerPrefs.SetInt ("Currency", PlayerPrefs.GetInt ("Currency", 0) - 1);
StartCoroutine (Resume ());
}
IEnumerator Resume()
{
yield return new WaitForSeconds (0.1f);
Player.SetActive (true);
Time.timeScale = 1;
Buttton.SetActive (false);
gameOver = false;
increasetime = true;
Gameoverpanel.SetActive (false);
DisableBoundary.SetActive (true);
}
Ok so far, even though the code is ugly as hell, when GameOver is called and player has more than 0 tokens, a Use Token Button is set active, if pressed, it start coroutine that resumes the game, and if the player has 0 tokens then gameOver is true and game is ended. now what i don’t know what do to is when the Resume button is set active, how to make a 10 seconds countdown in that if the player didn’t press the button then gameOver is true…
you need a UI Text gameObject to display the time.
in the gameManager script you need a coroutine to count down from 10 to 0, and update the UI Text.
When you enable the Resume button you need to start the coroutine. and stop the coroutine when the resume button is clicked.
something like this should work for the coroutine
public Ienumerator StartCountdown()
{
countdown = 10;
while (countdown >0)
{
yield return new WaitForSeconds(1.0f);
countdown --;
//update UIText.text gameObject
UIText.text = countdown.ToString();
}
/ /0 has been reached, = GameOver
//run the GameOver function
GameOver();
}