- Home /
Game Over GUI
I want to show a game over GUI without getting rid of anything else going on in the scene upon player death. But the player is destroyed upon their death, so when I add the GameOver component, it just gets destroyed right away (and gives me some errors). How can I get around this?
Here is what I have, although it does not work:
//Death is a component of the player.
//I have other stuff going on in death too,
//so getting rid of it is something I want to avoid
using UnityEngine;
using System.Collections;
public class Death : MonoBehaviour
{
private bool applicationClosing;
void OnApplicationQuit()
{
applicationClosing = true;
}
void OnDestroy()
{
if (applicationClosing)
{
return;
}
//GameOver is my Game Over GUI Script
gameObject.AddComponent<GameOver>();
}
}
Answer by zharik86 · Aug 27, 2014 at 06:49 AM
The simplest method, it to add new object on a scene. I hope that you destroy only one GameObject, instead of all scene:
void OnDestroy() {
if (applicationClosing) {
return;
}
//GameOver is my Game Over GUI Script
GameObject gmo = new GameObject(); //create new object
gmo.name = "myGUIscript"; //change name
gmo.AddComponent<GameOver>(); //add your component at object
}
I hope that it will help you.
Answer by HarshadK · Aug 27, 2014 at 06:50 AM
You are adding your GameOver component to your player object which is getting destroyed so once your player object is destroyed that component will not be available since it is destroyed with your player object. And you might be trying to access the GameOver component on your player object from other scripts.
So what you can do is create an empty game object and add your GameOver component to that object and then enable that game object from your Death script. So even if your player is destroyed your GameOver component is available to access for other scripts from the empty game object to which it is assigned to.
This can be done something like this:
public GameObject gameOverGameObject;
void OnDestroy()
{
if (applicationClosing)
{
return;
}
//GameOver is my Game Over GUI Script
gameOverGameObject.SetActive(true);
}
Here GameOverGameObject is the name of your empty game object which has the GameOver component attached to it already. Assing that Game Object to the gameOverGameObject variable from the script and also at the start set this game object to be disabled.
@Harshad$$anonymous$$ I am not sure, but GameObject.Find will not see deactivated objects(null reference exception)
Oh yeah! Didn't think of that. Thanks for noticing. Updated the answer.
I am a bit confused by this answer. If I already have a gameOverGameObject with a script attached to it, wont that script already run by default (before .setActive() is called)? Is it possible for me to create this gameObject and put my GameOver script inside of it inside of unity, and then just call gameOverGameObject.active(true); inside my code? Basically, to have it already exist but turned off, then to just flip a switch? (this was my original intent)
also, thanks for your answer :)
You need to set the game object to be disabled from the editor itself so that when you enable it, the attached script will be executed.
By that you mean uncheck it? I was unaware that this was a feature :)
Your answer
Follow this Question
Related Questions
GUI not showing up on player destroy 1 Answer
GUI Pop-Up On Cube Collision 1 Answer
Restart with GUIText 2 Answers
Player Screen Resolution 2 Answers
Organizing Player Prefs? 1 Answer