Search Unity

Help With Scene Changes

Discussion in 'Scripting' started by Nitrox32, Mar 10, 2021.

  1. Nitrox32

    Nitrox32

    Joined:
    May 9, 2017
    Posts:
    161
    I'm creating a save manager that does three basic things. 1) Load the first game scene from the start scene. 2) Load a saved game to the last saved scene and checkpoint. 3) Handle a scene change when the player goes through a portal to another scene. Numbers one and two are working as expected. I'm having a problem with number three.

    The save manager is a singleton object which is created in the start scene. This is the script that is attached to the portal in another scene. The portal's name is the name of the new scene.

    Code (CSharp):
    1. public class PortalName : MonoBehaviour
    2. {
    3.     private string portalSceneName;
    4.     private GameObject saveSystem;
    5.  
    6.     private void Awake()
    7.     {
    8.         saveSystem = GameObject.Find("SaveManager");
    9.         portalSceneName = gameObject.name;
    10.     }
    11.     public void ChangeScenes()
    12.     {
    13.         if (saveSystem != null)
    14.         {
    15.             saveSystem.GetComponent<GameSaveSystem>().ChangeScenes(portalSceneName);
    16.         }
    17.  
    18.         else
    19.         {
    20.             Debug.Log("Save Manager not Found!");
    21.         }
    22.     }
    23. }
    24.  
    This is the change scene script attached to the save manager:
    Code (CSharp):
    1.     public void ChangeScenes(string newSceneName)
    2.     {
    3.         UpdateProgressUI(0);
    4.         loadSceneCanvas.gameObject.SetActive(true);
    5.         LoadDSGameDataFromSceneChange();
    6.         StartCoroutine(BeginLoad(newSceneName));
    7.     }
    The problem is that the portal code is not finding the save manager so it can't pass the name of the object (which is the new scene's name) into the ChangeScenes script. Thanks for any help you can offer.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Are you properly marking it to persist from scene to scene?

    This is what I reach for:

    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://pastebin.com/SuvBWCpJ

    Unity3D Singleton with Prefab used for predefined data:

    https://pastebin.com/cv1vtS6G

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

    If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

    Code (csharp):
    1. public void DestroyThyself()
    2. {
    3.    Destroy(gameObject);
    4.    Instance = null;    // because destroy doesn't happen until end of frame
    5. }
     
  3. Nitrox32

    Nitrox32

    Joined:
    May 9, 2017
    Posts:
    161
    Ha, I figured it out. Yes, the singleton was set up correctly. The problem was in line eight. I was calling "SaveManager". I should have been calling "SaveManager Instance". Thanks for your response.
     
    Joe-Censored and Kurt-Dekker like this.