Search Unity

Getting the player to continue/revived ..

Discussion in 'Getting Started' started by CyberInteractiveLLC, Oct 14, 2017.

  1. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    307
    Hello, this is my script

    Code (CSharp):
    1. public class Destroy_By_Contact : MonoBehaviour
    2. {
    3.     public GameObject explosion;
    4.     public GameObject playerExplosion;
    5.     private Game_Controller gameController;
    6.     private DestroyByBoundary destroybyBoundary;
    7.     private StatsControl statscontrol;
    8.  
    9.     void Start ()
    10.     {
    11.  
    12.     void OnTriggerEnter(Collider other)
    13.     {
    14.         if (other.tag == ("Boundary") || other.CompareTag ("Enemy")) {
    15.             return;
    16.         }
    17.  
    18.         if (other.tag == "PowerUp") {
    19.             return;
    20.         }
    21.         if (explosion != null) {
    22.             Instantiate (explosion, transform.position, transform.rotation);
    23.         }
    24.         if (other.tag == "Player") {
    25.             Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
    26.             gameController.GameOver ();
    27.             Time.timeScale = 0.3f;
    28.             gameController.GetComponent<AudioSource> ().pitch = 0.4f;
    29.         }
    30.         Destroy (other.gameObject);
    31.         Destroy (gameObject);
    32.     }
    33.     void OnTriggerExit (Collider other)
    34.     {
    35.             if (other.tag == "Enemy") {
    36.                 return;
    37.             }
    38.     }
    39. }
    40.  

    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
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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.
     
    Sanhueza likes this.
  3. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    307
    Would it work the same if say i wanted to give the player option to watch an ad within 10 seconds to get revived?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yes.
     
  5. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    307
    Code (CSharp):
    1. public void GameOver ()
    2.     {
    3.         Gameoverpanel.SetActive (true); //if the game is over, shows the game over panel
    4.         DisableBoundary.SetActive (false); //disables boundary after player death so it stops counting score
    5.        
    6.    
    7.         increasetime = false;
    8.    
    9.         if (PlayerPrefs.GetInt ("Currency", 0) > 0) {
    10.             Buttton.SetActive (true);
    11.         } else {
    12.             gameOver = true;
    13.         }
    14.     }
    15.  
    16.     public void Button_Click ()
    17.     {
    18.         PlayerPrefs.SetInt ("Currency", PlayerPrefs.GetInt ("Currency", 0) - 1);
    19.         StartCoroutine (Resume ());
    20.     }
    21.  
    22.     IEnumerator Resume()
    23.     {
    24.         yield return new WaitForSeconds (0.1f);
    25.         Player.SetActive (true);
    26.         Time.timeScale = 1;
    27.         Buttton.SetActive (false);
    28.         gameOver = false;
    29.         increasetime = true;
    30.         Gameoverpanel.SetActive (false);
    31.         DisableBoundary.SetActive (true);
    32.     }
    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..
     
  6. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    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
    Code (CSharp):
    1. public Ienumerator StartCountdown()
    2. {
    3.      countdown = 10;
    4.      while (countdown >0)
    5.      {
    6.          yield return new WaitForSeconds(1.0f);
    7.           countdown --;
    8.          //update UIText.text gameObject
    9.         UIText.text = countdown.ToString();
    10.      }
    11. /    /0 has been reached, = GameOver
    12.     //run the GameOver function
    13.     GameOver();
    14. }