Search Unity

Problem with loading Game Over scene. Please help!

Discussion in 'Scripting' started by UnluckyProgrammer, Aug 16, 2017.

  1. UnluckyProgrammer

    UnluckyProgrammer

    Joined:
    Jun 25, 2017
    Posts:
    3
    Hi. I have a problem with loading Game Over scene. What I'm trying to do is when player dies, the game over scene appears with 1 second delay.
     

    Attached Files:

  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    So what is the problem you're having? By the way, it's preferable that you paste your code into the forums using code tags instead of uploading a screenshot of the code.
     
  3. UnluckyProgrammer

    UnluckyProgrammer

    Joined:
    Jun 25, 2017
    Posts:
    3
    I'm trying to make that when a player dies, the game loads game over scene but with a delay.
    This part of code loads the game over scene but i can't put in it "yield return new WaitForSeconds(2);"

    void Update()
    {
    if (Player == null)
    {
    SceneManager.LoadScene("GameOver");
    }
    }

    ...I know I should use IEnumerator in order to use "yield...." but I need something that is constantly checking if player is alive (that's why I used upadte function) and I don't know how.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Please use code tags when you're adding code to a post.

    You don't have to check if the player is dead every frame, you just have to check when he takes damage, right?

    Does your Player have a component that handles his health? You could add an event to that for "OnDamaged" or even more specifically "OnDead". Then you can have this GameController listen for that event, and then start the coroutine to show the game over scene.

    Here's an example of what I mean:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3. public class Player : MonoBehaviour {
    4.  
    5.     private float health;
    6.  
    7.     public UnityEvent OnDead;
    8.  
    9.     public void TakeDamage(float damage) {
    10.         health -= damage; // subtract from health
    11.         health = Mathf.Max(0f, health); // choose the greater value to keep health >= 0
    12.         // if theres no health left
    13.         if(health == 0f) {
    14.             OnDead.Invoke(); // fire the event
    15.         }
    16.     }
    17. }
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using System.Collections;
    4. public class GameController : MonoBehaviour {
    5.     public Player player; // reference to the Player component
    6.     public float gameOverDelay = 2f;
    7.     public string gameOverSceneName = "GameOver";
    8.  
    9.     private void Start() {
    10.         player.OnDead.AddListener(OnPlayerDead); // when this event is fired, call OnPlayerDead
    11.     }
    12.  
    13.     private void OnPlayerDead() {
    14.         player.OnDead.RemoveListener(OnPlayerDead); // stop listening for the dead event
    15.         StartCoroutine(GameOver());
    16.     }
    17.  
    18.     private IEnumerator GameOver() {
    19.         yield return new WaitForSeconds(gameOverDelay);
    20.         SceneManager.LoadScene(gameOverSceneName);
    21.     }
    22. }
     
    Last edited: Aug 16, 2017
  5. UnluckyProgrammer

    UnluckyProgrammer

    Joined:
    Jun 25, 2017
    Posts:
    3
    Hi :) so i finally got some time to test your code out. Didn't do it exactly like you wrote but you gave me an idea and it works. Thank you so much for your time and help, much appreciated.
     
    LiterallyJeff likes this.