Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Make game end after 120 seconds and load new scene?

Discussion in 'Scripting' started by MirioTogata, Oct 4, 2018.

  1. MirioTogata

    MirioTogata

    Joined:
    Oct 4, 2018
    Posts:
    5
    Hi,
    I've made my own sidescroller and also added a script that shows the time spent on playing.
    I want the game to end after 120 seconds and then load my gameover "Spiel vorbei" (german) scene. My question now is, how do I make it so that the scene is loaded as soon as the 120 seconds have passed?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PunkteAnzeigen : MonoBehaviour {
    6.     //öffentliche Felder für die beiden Steuerelemente zur Ausgabe
    7.     public UnityEngine.UI.Text timeLabel;
    8.     public UnityEngine.UI.Text pointsLabel ;
    9.  
    10.     float start = 0;
    11.     int points = 0;
    12.  
    13.     void Start () {
    14.         //die Startposition sichern
    15.         start = transform.position.x;
    16.     }
    17.  
    18.     void Update () {
    19.         int timeOut;
    20.         //die Distanz ermitteln
    21.         //dabei benutzen wir den absoluten Wert
    22.         points = (int) Mathf.Abs(transform.position.x -
    23.             start);
    24.         //durch die Konvertierung gehen Nachkommastellen
    25.         //verloren
    26.         timeOut = (int) Time.time;
    27.         //und nun ausgeben
    28.         timeLabel.text = timeOut.ToString();
    29.         pointsLabel.text = points.ToString();
    30.     }
    31. }
    32.  
     

    Attached Files:

    Last edited: Oct 4, 2018
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    3,574
    Code (csharp):
    1. private float timeUntilSceneEnds = 120f;
    2. private float timeWhenSceneEnds;
    3.  
    4. void Start ()
    5. {
    6.     timeWhenSceneEnds = Time.time + timeUntilSceneEnds;
    7. }
    8.  
    9. void Update ()
    10. {
    11.     if (timeWhenSceneEnds <= Time.time)
    12.     {
    13.         SceneManager.LoadScene("GameOverScene");
    14.     }
    15. }
    You can also do this with a coroutine, but doing it in update is generally the easiest way (especially for beginners, not sure if that is you) to understand. Using Invoke would also be another way of doing it.
     
  3. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    94
    You can load a scene using SceneManager.LoadScene (https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html)
    You could either use Time.time as you already did and just make use of an if, example:
    (For this way best look at the code Joe provided in his reply)
    Code (CSharp):
    1. if (Time.time >= 120){
    2.     SceneManager.LoadScene(sceneName);
    3. }
    Or you could make a Coroutine (https://docs.unity3d.com/ScriptReference/Coroutine.html) that waits for 120 seconds (https://docs.unity3d.com/ScriptReference/WaitForSeconds.html) and then does that. For Example:
    Code (CSharp):
    1. void Start () {
    2.     //die Startposition sichern
    3.     start = transform.position.x;
    4.  
    5.     //die Coroutine starten
    6.     StartCoroutine(LoadSceneAfterSeconds);
    7. }
    8.  
    9. IEnumerator LoadSceneAfterSeconds() {
    10.     yield return new WaitForSeconds(120);
    11.     SceneManager.LoadScene(sceneName);
    12. }
     
  4. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    428
    The above solutions will work well. Just thought I'd chime in with my goto solution for this kind of thing:

    https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         Invoke("GameOver", 120);
    4.     }
    5.  
    6.     void GameOver()
    7.     {
    8.             SceneManager.LoadScene("Spiel vorbei");
    9.     }
    You can also use CancelInvoke("GameOver"); to stop this for any reason.
     
    Joe-Censored likes this.
  5. MirioTogata

    MirioTogata

    Joined:
    Oct 4, 2018
    Posts:
    5
    thanks for your help!
     
  6. MirioTogata

    MirioTogata

    Joined:
    Oct 4, 2018
    Posts:
    5
    thanks for your help!
    thanks for your help!