Game Over GUI Question

I have a game in which the first person controller is destroyed when the player enters into a collider of an enemy. I also have a game over GUI script that I want to run when the player dies. I have tried using Boolean variables, but the player is destroyed and the scripts are destroyed too so I have not found a good way to do so. What is the best way to have a player die without destroying it? Or what is a good way to create Game Over GUI?

You know, man, death doesn’t have to be THAT bad. It can be mostly a state of mind… :slight_smile:

Now seriosly, instead of actually destroying the player controller, make dead “just a state of mind”.
That is, add a bool IsDead property to the player character.

When it’s false, the game runs as usual.

When it’s set to true, make the camera unattach itself from the player character (or stop following it, or whatever you are doing), and make the player character disable it’s renderers and it’s scripts (by setting their enabled field to false).

That will make the character disappear and stop processing input, and colliding, etc.
Then you can move the camera somewhere else or make a Game Over UI appear with buttons for restarting or quitting.

Restarting would do something like:

  • Place the character object back to starting position.
  • Reset its stats (health, ammo, whatever).
  • Reenable all its components and renderers. It will make it appear and start running its scripts and collide and everything.
  • Attach the camera back to it, or make it follow it again, or whatever you do with your camera.

Create a GameManager that can set the state of the game and reports it.

public enum State{Running, Pause, Over}
public class GameManager
{
    public State gameState;
    void Start(){
        _gaemeState = State.Running;
    }
}

Then create an empty game object and add this to it.
From any script that needs to know the state of the game:

public GameManager manager;

void Update(){
   if(manager.gameState == State.Pause)return;
   if(health = 0) 
   {
       manager.gameState = State.Over;
       Destroy(gameObject);
   }
}

In the Pause script

public GameManager manager;
void OnGUI(){
   if(manager.gameState == State.Over)GameOverGUI();
}

You can also make each level to inherit from GameManager so that it is a manager itself and has access to all other members you would add.