- Home /
Gameover function calling before game ends help
Hey Unity Community i need some help wondering why my script is calling before the player dies. I have a gameover script that calls after the player loses all their lives and for some reason its called before the player dies. Here is the script for reference
Game Manager script
public class GameController : MonoBehaviour
public Gameover GameOverScript;
public int playerLives = 2;
public GameObject player;
public Texture shipTexture;
void Update()
{
if (player == null && playerLives >= 1) {
playerLives--;
player = ((Transform)Instantiate (playerShip, new Vector3 (0, 0, 0), playerShip.transform.rotation)).gameObject;
}
}
void OnGUI()
{
if (playerLives != 0)
{
for (int i=1; i<=playerLives; i++)
{
GUI.DrawTexture (new Rect (i * 36, 0, 750, 48), shipTexture, ScaleMode.ScaleToFit, true);
}
}
if (playerLives == 0 && player == null)
{
GameOver();
}
}
void GameOver()
{
GameOverScript = GameObject.FindGameObjectWithTag ("GameOver").GetComponent<Gameover>();
}
Game Over script
void OnGUI()
{
const int buttonWidth = 120;
const int buttonHeight = 60;
if (
GUI.Button(
// Center in X, 1/3 of the height in Y
new Rect(
Screen.width / 2 - (buttonWidth / 2),
(1 * Screen.height / 3) - (buttonHeight / 2),
buttonWidth,
buttonHeight
),
"Retry!"
)
)
{
// Reload the level
Application.LoadLevel("Stage1");
}
if (
GUI.Button(
// Center in X, 2/3 of the height in Y
new Rect(
Screen.width / 2 - (buttonWidth / 2),
(2 * Screen.height / 3) - (buttonHeight / 2),
buttonWidth,
buttonHeight
),
"Back to menu"
)
)
{
// Reload the level
Application.LoadLevel("Menu");
}
}
I have the Gameover script attached to an empty game object is thats why its called earlier? Here is a pic of how it looks like. If you can help with this thank you!
Have you tried to disable the Gameover script in the Awake() function on the Gameover script, then try to enable it in your Gameover function of your Game manager script? Something like:
void GameOver()
{
GameOverScript = GameObject.FindGameObjectWithTag("GameOver").GetComponent<Gameover>();
GameOverScript.SetEnabled(True);
}
I'm not 100% sure on this, but I don't know if you need the GameObject.FindGameObjectWithTag... line because you declared it public and will be setting it in the editor. I could be wrong though.
Answer by kamgru123 · Oct 16, 2014 at 10:37 PM
You have no mechanism to trigger the gameover. Your OnGUI method is being called right from the start. Best way would be to add a boolean to your gameover script that would check whether it should perform the OnGUI. Something like this:
class GameOverScript : MonoBehaviour
{
public bool GameOver { get; set; }
void OnGUI()
{
if (!GameOver)
return;
//rest of your code...
}
}
Then in your Game manager script in the GameOver() method you set the bool to true:
void GameOver()
{
GameOverScript = GameObject.FindGameObjectWithTag ("GameOver").GetComponent<Gameover>();
GameOverScript.GameOver = true;
}
Answer by Michael-Thomas · Oct 16, 2014 at 10:34 PM
If I understand correctly that you've attached the GameOver script to an empty object and it's showing up immediately on game start then that makes perfect sense. The OnGUI method is always called while that object exists and it will always exist.
One solution would be to remove the GameOver script from the empty object and replace the GameOver() method in your GameController with:
private void GameOver () {
AddComponent<GameOver>();
}
This will attach the GameOver component to the same object your GameController is attached to, but only after the correct conditions are met.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Floating Enemy Health Bars? 1 Answer
Unity3D Game Time 1 Answer
Is it possible to link character skill lists to a GUI, and if so, how? 3 Answers
How To Make GUI Buttons Load/Quit 1 Answer