Ok, so what you know now is that the GameOver
method is only being called by the CollisionHandler.cs script and that that script is only on the Player Ship. It lists the GameController because its a method within the GameController.cs script which is attached to that object.
We know we can make changes now without messing up lots of things.
So, next steps…
- Within GameController.cs, remove this line of code;
private bool restart = false;
- Within the
OnGUI
method, remove these lines of code;if (restart) { if (Input.GetKeyDown(KeyCode.R)) { Invoke("SceneManager.LoadScene(1)", 2f); } if (Input.GetButtonDown("Restart")) Invoke("SceneManager.LoadScene(1)", 2f); }
- Within the
SpawnWaves
method, remove these lines of code;if (gameOver) { restartText.text = "Press 'R' for Restart"; restart = true; break; }
- Update the
Update
method as follows;void Update() { if (enemiesLeft == 0) { endGame(); } if (gameOver) { if (Input.GetKeyDown(KeyCode.R)) { Debug.Log("Restart Game"); } } }
- Within the
GameOver
method, add this line of code (ideally where you set the other text);outcome.text = "You Lose"; restartText.text = "Press 'R' for Restart";
- Select the Restart UI Text GameObject
- Set its width to 400
- Set the font size to 50
When you run the game, crash into an enemy ship, you’ll see the “Game Over” text, the “You Lose” text and the “Restart” text, press R and you’ll see a message in the console.