Search Unity

My Game over isn't working, but I don't have any errors

Discussion in 'Scripting' started by Neon_Apollo, Jan 27, 2018.

  1. Neon_Apollo

    Neon_Apollo

    Joined:
    Dec 8, 2017
    Posts:
    13
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class SpaceSceneManager : MonoBehaviour
    6. {
    7.  
    8.     public Camera MainCamera;
    9.     public Text ScoreText;
    10.     public Text GameOverText;
    11.     public ShipControl Ship;
    12.  
    13.     int score = 0;
    14.     float GameTimer;
    15.     float EnemyTimer;
    16.     public bool GameOver;
    17.  
    18.     void Update()
    19.     {
    20.         GameOverLogic();
    21.         TimerLogic();
    22.     }
    23.  
    24.  
    25.     void GameOverLogic()
    26.     {
    27.         if (GameOver)
    28.         {
    29.             if (Input.GetKeyDown("r"))
    30.             {
    31.                 SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    32.             }
    33.             {
    34.                 if (Input.GetKeyDown("escape"))
    35.                 {
    36.                     SceneManager.LoadScene("Menu");
    37.                 }
    38.             }
    39.  
    40.             ScoreText.enabled = false;
    41.             GameOverText.enabled = true;
    42.  
    43.             GameOverText.text = "Game Over! Total Score: " + score + "\nPress R to restart! \nPress ESC to quit";
    44.  
    45.             return;
    46.         }
    47.     }
    48.  
    49. public void OnGameOver()
    50.     {
    51.         GameOver = true;
    52.         Time.timeScale = 0.3f;
    53.     }
    54. }
    Here's a bit of my script, I am making a project through a tutorial course, the teacher hasn't answered when I asked about what might be wrong, so I'm asking here. GameOver is never activated. Only thing I can think of is that it looks like (to me, anyway) GameOver is never actually given a reason to activate, but I'm still quite new to this so I don't know.
    Thanks for answers!
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sure, well if "OnGameOver" is never called by anything, you'll never reach the condition.

    With just what's posted, it's not easy to say more. Presumably that method should be called by something.. somewhere... :)
     
  3. Neon_Apollo

    Neon_Apollo

    Joined:
    Dec 8, 2017
    Posts:
    13
    thanks for the quick reply methos! Do you want me to post more of the script? That's the only part from any of my scripts that deals with the GameOver, and how would I go about calling on it? (for extra context the GameOver in the course I'm in fires as soon as a player in the scene is destroyed)
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, perhaps post the part where the player is destroyed (if it's hit or falls or whatever ends it, I have no idea).
    You can post the code, or simply try to call that method from when "it's destroyed" and that should work. :)

    If you get stuck, feel free to post whatever you feel is relevant and I/someone else will have a look ,for sure :)