All my game data like gameplay objects and main menu in the same scene.
I have in my main menu buttons one of them is PLAY and should start a new game.
All the gameplay objects are under one gameobject name Game Data and the main menu objects are under Main Menu. And when I click the PLAY button I setactive true/false the Game Data and the Main Menu.
When running the game the game start when Game Data is setactive false and Main Menu setactive true.
Then in the Main Menu I click on PLAY and the game start.
The problem is while the game is running when I hit the escape key and get back to the main menu if I click the PLAY button again to start a new game it will continue from the last point and will not start over again.
I thought to try to re load the same current scene but it’s not working.
The MainMenu script with the function PlayGame that is called from the PLAY button :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenu : MonoBehaviour
{
public GameObject gameData;
public GameObject mainMenu;
public void PlayGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
gameData.SetActive(true);
mainMenu.SetActive(false);
}
public void QuitGame()
{
Application.Quit();
}
}
I have one scene so I tried to add this line :
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
But then it start the game but after a second or less it stop the game and make the main menu set active true and the game data set active false. Even if I set the game data to be true and main menu false :
gameData.SetActive(true);
mainMenu.SetActive(false);
Why when starting a new game by loading the scene it’s setting the game data to be false and the main menu to be true if I set them true and false ?
This is a screenshot of my Hierarchy :
So what I want to do is when I click the Play Button to start a new game. Reset everything. Loading the scene over again is good idea I think since it reset and start everything over again. But for some reason after clicking the PLAY button it’s starting the game for a second or less and then return to this like in the screenshot. The Main Menu is setactive true and Game Data is setactive false.
But I want that the Main Menu will be setactive false and the Game Data setactive true.
And I set it in the PlayGame function but for some reason it’s not working it keep setting main menu true and game data false.
This is the back to main menu script using the escape key :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class BackToMainMenu : MonoBehaviour
{
public GameObject gameData;
public GameObject mainMenu;
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
gameData.SetActive(false);
mainMenu.SetActive(true);
}
}
}
