0
\$\begingroup\$

My Hierarchy have 3 main GameObjects :

  1. Game Data where all the gameplay objects are stored as childs of the Game Data GameObject.

  2. Main Menu and all objects of the main menu stored as childs under the Main Menu GameObject.

  3. Game Manager GameObject that have a script that control the game like pausing the game or go to the main menu.

Hierarchy

By default the Game Data is disabled and all the gameplay objects childs disabled too. Then when starting the game with the main menu when clicking the button to start a new game I'm just activating the Game Data object :

This script is attached to the Main Menu :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MainMenu : MonoBehaviour
{
    public GameObject gameData;

    public void StartNewGame()
    {
        gameData.SetActive(true);
    }

    public void ResumeGame()
    {

    }

    public void QuitGame()
    {
        Application.Quit();
    }
}

This is working for first time starting a new game.

And this script is attached to the Game Manager GameObject :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameController : MonoBehaviour
{
    public static bool gamepaused = false;

    public GameObject gameData;
    public GameObject mainMenu;

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.P))
        {
            gamepaused = !gamepaused;

            if (gamepaused)
            {
                Time.timeScale = 0f;
            }
            else
            {
                Time.timeScale = 1f;
            }
        }

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            gameData.SetActive(false);
            mainMenu.SetActive(true);

            Time.timeScale = 0f;
        }
    }
}

This script is working. When pressing the P button the game pause/continue. When pressing the ESCAPE key the game also pause but also back to the main menu by switching the gameobjects disable/enable.

Now let's say I pressed the escape key while playing the game and I'm back to the main menu now I want to start a new complete game but clicking the StartNewGame ui button.

The problem is that this time I don't want to just active the Game Data like I did before :

gameData.SetActive(true);

This time at least in my logic I want to destroy the existing Game Data and instantiate a new Game Data using a prefab I already have of the Game Data. This is my logic but I'm not sure how to do it. Or something like that.

The main goal is to be able to start over a new game each time clicking the StartNewGame ui button.

\$\endgroup\$
1
\$\begingroup\$

You could just reload scene (I presume you have one scene in the game that contains all objects and logic?) using SceneManager.LoadScene(0).

This will load the scene with build index '0' (the first scene in the game). You can find additional info about scenes in build here: https://docs.unity3d.com/Manual/BuildSettings.html

After loading scene all scene data will reset to default state.

\$\endgroup\$
  • \$\begingroup\$ I have 3 scenes one with all gameplay objects one the main menu and one a manager scene. The problem is when I hit the escape key to go back to the main menu and loading the main menu scene and it's removing the gameplay scene I will not be able to continue the game. Isn't it should be that when you go back to the main menu the game should be paused and then you can decide in the main menu if to resume the game or to start a new one ? This is the problem I don't understand. What is the logic here and how should I do it ? \$\endgroup\$ – Daniel Lip Dec 10 '19 at 8:07
  • \$\begingroup\$ Since if you hit the escape key and it's loading the main menu scene then all the game progress destroyed and you can't continue the game. In most of the games when you go back to the main menu you can also resume the game. \$\endgroup\$ – Daniel Lip Dec 10 '19 at 8:08
  • \$\begingroup\$ Are you sure you are talking about scenes, not scene objects? In your screenshot in original post i see that you have a scene with three objects in it: game data, main menu, and gamemanager. You can enable and disable your objects without reloading a scene and keep all three of them together in single scene. But when you're reloading whole scene, all data is reset to default state - like starting a new game. So when you hit ESC, you're disabling data object inside the scene, and when you hit "new game", you reloading whole scene. That's the difference. \$\endgroup\$ – Xamtouo Dec 10 '19 at 12:35
  • \$\begingroup\$ Well, in retrospective, this approach may be unnecessarily complex for your task. Probably all you need is to create Prefab of your Game Data, and when player chooses new game, you Instantiate new Game Data and Destroy old one. \$\endgroup\$ – Xamtouo Dec 10 '19 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.