Search Unity

End game when time is up and/or when count is over

Discussion in 'Scripting' started by cjchoi0519, Feb 23, 2018.

  1. cjchoi0519

    cjchoi0519

    Joined:
    Feb 23, 2018
    Posts:
    4
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     public Text countText;
    9.     public Text winText;
    10.  
    11.     private Rigidbody rb;
    12.     private int count;
    13.  
    14.     public int timeLeft = 30;
    15.     public Text countdownText;
    16.  
    17.     void Start ()
    18.     {
    19.         rb = GetComponent<Rigidbody> ();
    20.         count = 0;
    21.         SetCountText ();
    22.         winText.text = "";
    23.         StartCoroutine("LoseTime");
    24.  
    25.     }
    26.  
    27.     void FixedUpdate()
    28.     {
    29.         float moveHorizontal = Input.GetAxis ("Horizontal");
    30.         float moveVertical = Input.GetAxis ("Vertical");
    31.  
    32.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    33.  
    34.         rb.AddForce (movement * speed);
    35.     }
    36.  
    37.     void OnTriggerEnter(Collider other)
    38.     {
    39.         if (other.gameObject.CompareTag ("Pick Up"))
    40.         {
    41.             other.gameObject.SetActive (false);
    42.             count = count + 1;
    43.             SetCountText ();
    44.      
    45.         }
    46.     }
    47.     void SetCountText ()
    48.     {
    49.         countText.text = "Count: " + count.ToString ();
    50.         if (count >= 13)
    51.         {
    52.             winText.text = "You Win!";
    53.         }
    54.     }
    55.  
    56.     void Update()
    57.     {
    58.         countdownText.text = ("Time Left = " + timeLeft);
    59.  
    60.         if (timeLeft <= 0)
    61.         {
    62.             StopCoroutine("LoseTime");
    63.             countdownText.text = "Times Up!";
    64.         }
    65.     }
    66.  
    67.     IEnumerator LoseTime()
    68.     {
    69.         while (true)
    70.         {
    71.             yield return new WaitForSeconds(1);
    72.             timeLeft--;
    73.         }
    74.     }
    75. }
    76.  
    I have a working code here based on roll a ball tutorial.
    I would like the game to be over when either time is up and/or I finish collecting all the objects...

    What changes should I make?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    So what isn't working, and what exactly are you looking for it to do differently? "End game" can mean a lot of things. It could mean going back to the main menu, it could mean starting a new game, it could mean exiting the application, etc. Looks like right now you're just setting some text values.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It already looks like you have that, mostly. Do you want to prevent movement or collection if time expires (or stop the timer counting down if you get all of the objects picked up)?
     
  4. cjchoi0519

    cjchoi0519

    Joined:
    Feb 23, 2018
    Posts:
    4
    I meant starting a new game when I said "End Game".
     
  5. cjchoi0519

    cjchoi0519

    Joined:
    Feb 23, 2018
    Posts:
    4
    Yes. I would like to prevent movement or collection if time expires. Moreover, I would like to start a new game when I am done collecting all the objects within the time.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  7. cjchoi0519

    cjchoi0519

    Joined:
    Feb 23, 2018
    Posts:
    4
    To be honest, I don't know how to set a bool for this. Would you please breifly tell me how?
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    When time's up or you've collected everything, maybe a "gameFinished = true"?
    In FixedUpdate or TriggerEnter you can check if that bool's true, and just 'return' if it is (skipping the code).

    Then, show your button to allow the level to restart..

    Let me know if that makes sense :)