- Home /
How to display a "Game Over" after death?
I would like to code my game so that when my player dies(or is set inactive), black text shows up in the middle of the screen displaying "Game Over!"
How is this possible?
Answer by morgan23 · Feb 23, 2014 at 01:25 AM
you could do it in script just make a bool that checks when player is dead then display a GUI or a GUItext.
Answer by morgan23 · Feb 23, 2014 at 01:53 AM
I can give you an example script for one option I have no experience with the game object GUItext. Here is a script I made for respawning my player when he dies. I will put a comment for what part you might be interested in this should work for what you want and if some stuff needs explaining I can help. public class PlayerRespawn : MonoBehaviour { //Public Variables
public float ReSpawnTime = 0f;
private int RespawnIndex= 0;
private Camera MainCamera;
public Transform[]ReSpawnLocation = new Transform[4];//Array For All My Repsawn Locations
private PlayerVitals Pv;
private GameGUI Gg;
private bool ShowRespawnButton = false;
public GameObject Player;
//public Vector3 CameraPos = new Vector3(0,0,0);
void Awake()
{
MainCamera = Camera.main; // Grab a reference to the camera
//MainCamera = GameObject.FindWithTag("MainCamera");
Gg = MainCamera.GetComponent();
//Pv = GetComponent();
Pv = GameObject.Find("Player").GetComponent();
//Pv = Player.GetComponent();
}
// Use this for initialization
void Update()
{
if(Pv.Life <= 0)
{
Dead(IsDead:true);
//Debug.Log (ReSpawnLocation[RespawnIndex].name);
}
}
**void OnGUI()
{
if(ShowRespawnButton)//This would be what you want
if(GUI.Button(new Rect(10,10,75,40),"Respawn"))
{**
StartCoroutine("Respawn");
}
}
//Method That ReSpawns The Player
IEnumerator Respawn()
{
yield return new WaitForSeconds(ReSpawnTime);
RespawnIndex = Random.Range(0,ReSpawnLocation.Length);
Player.transform.position = ReSpawnLocation[RespawnIndex].position;
MainCamera.transform.position = Player.transform.position;
MainCamera.transform.localPosition = new Vector3(0f,0f,-15f);
Pv.Life = Pv.MaxLife;
Dead (false);
yield return 0;
StopCoroutine("Respawn");
//Debug.Log ("Respawned");
}
private void Dead (bool IsDead)
{
if(IsDead ==true)
{
ShowRespawnButton = true;
Gg.ShowGui = false;
Player.renderer.enabled = false;
//Debug.Log("you died");
}
if(IsDead==false)
{
ShowRespawnButton = false;
Player.renderer.enabled = true;
//StartCoroutine("MoveToTarget");
//Debug.Log(IsDead);
}
}
}
Trying to use your script but its really old. I tried to play around it and fix some things but i am not really good at program$$anonymous$$g so it didn't succeed. Do you have a more updated version ? If yes can you share it with me ? I've searched for like a week to kill and respawn my player and show a game over screen. Even tutorials couldn't help me. I hope you will have time to help me.
Your answer
Follow this Question
Related Questions
How do i centralize one string in any viewport size? 1 Answer
Make UI always center the panel 1 Answer
Adding vertices to TextMesh Pro 0 Answers
How to align text? 1 Answer