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!

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.

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;
    }