Hello, I’ve created a simple app with multiple mini games. In these games, I have the usual “game over!” with restart button, which works as intended.
The problem I’m running into is when I switch to “back to menu” scene and restart the game, I’m switched to the game scene, but the game objects do not load.
This is what I have in my game manager script. A noobie hoping to get some guidance!
public void RestartGame()
{
Debug.Log("RestartGame called");
// Clear existing enemies
foreach (GameObject enemy in GameObject.FindGameObjectsWithTag("Enemy"))
{
Destroy(enemy);
Debug.Log("Enemy destroyed in RestartGame");
}
// Reset game state
isGameOver = false;
score = 0;
gameOverPanel.SetActive(false);
// Re-initialize the game
SpawnPlayer();
StartCoroutine(IncrementScore());
InvokeRepeating("SpawnEnemy", spawnTime, spawnTime);
}
public void BackToMenu()
{
Debug.Log("BackToMenu called");
// Clear existing enemies
foreach (GameObject enemy in GameObject.FindGameObjectsWithTag("Enemy"))
{
Destroy(enemy);
Debug.Log("Enemy destroyed in BackToMenu");
}
// Clear player
if (player != null)
{
Destroy(player);
Debug.Log("Player destroyed in BackToMenu");
}
// Reset game state
isGameOver = false;
score = 0;
gameOverPanel.SetActive(false);
CancelInvoke("SpawnEnemy");
SceneManager.LoadScene("PracticeModeScene");
Destroy(gameObject); // Ensure the GameController is properly reset
}
}