How can I start a new game with loading the same current scene that is already loaded ?

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

If you call LoadScene and then try to do something to the gameobjects in the scene, it’s not going to work. The code doesn’t load the scene and then continue to execute the code after it.

You can just reload the scene, but I would set a static variable to indicate I’m restarting and not doing a new playthrough. Just change the value before you call loadscene. Then when the scene loads, you check against this flag ( a simple bool will work) and determine what you want to set active.

The other option is to have a way to simply reset everything. But that can take a bit of work depending on what you would need to reset or it can be pretty simple.

Can you show me example ?

Where do I init the static bool flag ? Should it be public ? Where do I make the indication if it’s restarting a new game or not and how ?

Do this: See that hierarchy that is your game view, GameData?? Don’t just use it the way it is now.

Instead, call .SetActive(false) on it permanently, and when you start a new game, use that reference to that object and Instantiate() it into a shiny new copy of it.

When you .SetActive() on that copied hierarchy it will be a copy of the original turned off GameData.

At GameOver, just call Destroy() on that copy, while keeping the original for the next game.

I love Unity3D. So elegant!