Search Unity

Is there any easy way to reset the scene to default when starting a new game ?

Discussion in 'Editor & General Support' started by SharonL75, Oct 1, 2020.

  1. SharonL75

    SharonL75

    Joined:
    Aug 13, 2020
    Posts:
    91
    The problem is when the game is running I'm making a loading for my saved game and then while the game is still running I'm doing escape key to main menu and then starting a new game. The new started game remember the saved changes.

    Only if I quit the game the app it self and running the game over again for example if I quit the game and running again the built exe file then making a new game it will start a new game by default all objects are in the default positions rotations states.

    But if I'm doing a new game when the game is running and after I loaded a saved game then the new game is starting like I loaded a saved game and not like a new game.

    It's a bit annoying. I can start writing each object state and position rotation scaling and other stuff and then store it back on a new game but what if I have like 100 objects in the world ? And what if I have some cameras like free look cameras it will take a lot of time to get each camera properties and store them and load them back when making a new start new game.

    I have two scenes in my game "Main Menu" and "Game" and when I'm loading a saved game I'm first loading the "Game" scene and then loading the saved game to apply the changes.

    But when I'm doing start a new game I'm loading the same scene "Game" without loading the saved game but since I never quitted the game it's loading the "Game" scene with the changes made.

    For example here I'm doing the Load and the Save :
    For now I'm saving and loading only the Player position and rotation.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Cinemachine;
    6. using UnityEngine.UI;
    7. using UnityEngine.SceneManagement;
    8. using SpeedTutorMainMenuSystem;
    9. using Lowscope.Saving;
    10. using System.IO;
    11.  
    12. public class PlayingInGameScenesController : MonoBehaviour
    13. {
    14.     public CinemachineFreeLook[] freeLookCams;
    15.     public CinemachineVirtualCamera[] virtualCams;
    16.     public LockController lockController;
    17.     public GameObject player;
    18.     public GameObject uiSceneText;
    19.     public float transitionSpeed = 5f;
    20.     public float thresHold = 0.1f;
    21.     public bool talkingProcess = true;
    22.     public FadeInOutSaveGameText fadeInOutSaveGame;
    23.  
    24.     private bool newGame = true;
    25.     private bool loaded = false;
    26.  
    27.     private void Start()
    28.     {
    29.      
    30.     }
    31.  
    32.     public void PlayingSceneInGame()
    33.     {
    34.         PlayingSceneStatesControls(true);
    35.         StartCoroutine(ScenePlayingTime());
    36.     }
    37.  
    38.     private void Update()
    39.     {
    40.         if (SceneManager.GetActiveScene().name != "Main Menu" && newGame == true && MenuController.loading == false)
    41.         {
    42.             var playerStart = GameObject.Find("Player Start");
    43.             player.transform.position = playerStart.transform.position;
    44.             player.transform.localScale = playerStart.transform.localScale;
    45.             player.transform.rotation = playerStart.transform.rotation;
    46.            
    47.  
    48.             PlayingSceneInGame();
    49.             newGame = false;
    50.         }
    51.  
    52.         if (SceneManager.GetActiveScene().name != "Main Menu" && MenuController.loading == true && loaded == false)
    53.         {
    54.             SaveLoad.Load();
    55.             loaded = true;
    56.         }
    57.     }
    58.  
    59.     private void LateUpdate()
    60.     {
    61.        
    62.     }
    63.  
    64.     private void PlayingSceneStatesControls(bool LockState)
    65.     {
    66.         lockController.LockControl(LockState);
    67.  
    68.         if (LockState == true)
    69.         {
    70.             uiSceneText.SetActive(true);
    71.         }
    72.         else
    73.         {
    74.             talkingProcess = false;
    75.             uiSceneText.SetActive(false);
    76.         }
    77.     }
    78.  
    79.     IEnumerator ScenePlayingTime()
    80.     {
    81.         yield return new WaitForSeconds(10);
    82.  
    83.         PlayingSceneStatesControls(false);
    84.  
    85.         freeLookCams[0].enabled = false;
    86.         freeLookCams[1].enabled = true;
    87.  
    88.         var brain = Camera.main.GetComponent<CinemachineBrain>().m_DefaultBlend.m_Time;
    89.  
    90.         StartCoroutine(SaveAfterSomeTime());
    91.        
    92.     }
    93.  
    94.     IEnumerator SaveAfterSomeTime()
    95.     {
    96.         yield return new WaitForSeconds(15f);
    97.  
    98.         SaveLoad.Save();
    99.  
    100.         StartCoroutine(fadeInOutSaveGame.OverAllTime(5f));
    101.     }
    102. }
    103.  
    104. So to store back the player to his original position and rotation and scaling when making a start new game I had to do it manual :
    105.  
    106. [code]
    107. var playerStart = GameObject.Find("Player Start");
    108.             player.transform.position = playerStart.transform.position;
    109.             player.transform.localScale = playerStart.transform.localScale;
    110.             player.transform.rotation = playerStart.transform.rotation;
    111.  
    "Player Start" is just an empty GamerObject in the scene at the same Player position and rotation.
    So this work around for the Player is working but now to do it for each object and camera in the world ?

    This is in the main menu scene script two method I'm calling from OnClick buttons for new game start and load :

    Code (csharp):
    1.  
    2. public void ClickNewGameDialog(string ButtonType)
    3.         {
    4.             if (ButtonType == "Yes")
    5.             {
    6.                 loading = false;
    7.                 newGameDialog.SetActive(false);
    8.                 StartCoroutine(sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, _newGameButtonLevel));
    9.             }
    10.  
    11.             if (ButtonType == "No")
    12.             {
    13.                 GoBackToMainMenu();
    14.             }
    15.         }
    16.  
    17.         public void ClickLoadGameDialog(string ButtonType)
    18.         {
    19.             if (ButtonType == "Yes")
    20.             {
    21.                 loading = true;
    22.                 newGameDialog.SetActive(false);
    23.                 StartCoroutine(sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, _newGameButtonLevel));
    24.             }
    25.  
    26.             if (ButtonType == "No")
    27.             {
    28.                 GoBackToMainMenu();
    29.             }
    30.         }
    31.  
    And the FadeAndLoadScene script :
    This is where I'm loading the "Game" scene when doing Loading for saved game or for starting a new game in both cases it's loading the "Game" scene !

    Code (csharp):
    1.  
    2. public IEnumerator FadeAndLoadScene(FadeDirection fadeDirection, string sceneToLoad)
    3.     {
    4.         yield return Fade(fadeDirection);
    5.         SceneManager.LoadScene(sceneToLoad);
    6.     }
    7.  
    The problem again and when it's working and when it's not :

    * When running the game first time and clicking the New Game button the game load the scene "Game" and everything is working fine !

    * When quitting the game closing the app it self if in the editor or in the build and then running the app/build exe file over again and making start a new game it's working fine !

    * When the game is already running and I'm in the main menu and I'm clicking the Load button and it's loading the "Game" scene and then loading the saved file and make the changes like put the player in some other position, then if I'm going to the main menu and click on Start new game it will load the "Game" scene but will keep the loaded changes from the saved game file. It will not reset the world the objects to the default !
     
  2. DreamPower

    DreamPower

    Joined:
    Apr 2, 2017
    Posts:
    103
    My suggestion: try it without the Coroutine like, just call SceneManager.LoadScene(sceneToLoad); from a normal function and see if that fixes the problem. Also, if you have any static variables, those will not get reset when loading a new scene. And if you are relying on PlayerPrefs for your save data, don't forget to save them (PlayerPrefs.Save) - Save is automatically called in OnApplicationQuit, but if you want to save them earlier you have to do it yourself.