Saving system and start menu

I am designing a mobile application and with the saving wrapper and everything that we were provided with. There is a load last scene function but I don’t want to do it that way. First comes my start scene then the load scene and then the game scene. So I hooked up the start button to load the load scene
And I made some code like this to make sure load scene loads the default scene and in the starting it doesn’t load anything but a new game.

IEnumerator LoadLast()
    {
        if(GetComponent<SavingSystem>().LoadLastScene(defaultSaveFile)!=null)
        {
            yield return GetComponent<SavingSystem>().LoadLastScene(defaultSaveFile);
        }
        else
        {
            AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(2);
            while (!asyncLoad.isDone)
            {
                yield return null;
            }
        }
    }

but it is showing object reference error when starting for the first time. But I am checking if it is null or not

The problem with this null check is that if SavingSystem is null, then finishing the statement will result in a null reference error.

Are you wanting to test if a save file exists?

yes

Add this method to SavingSystem:

 public bool FileExists(string saveFile)
 {
        string path = GetPathFromSaveFile(saveFile);
        return FileExists(path);
 }

Now your LoadLast() should also have protection, to make sure you have a SavingSystem attached:

IEnumerator LoadLast()
{
      SavingSystem savingSystem = GetComponent<SavingSystem>();
      if(savingSystem)
      {
           if(savingSystem.FileExists(defaultSaveFile)
          {
                yield return GetComponent<SavingSystem>().LoadLastScene(defaultSaveFile);
                return;
          }
          else
          {
               Debug.Log("No Saving System!");
         }
       AsynchOperation asynchLoad=SceneManager.LoadSceneAsync(2);
       while(!asynchLoad.IsDone)
       {
             yield return null;
       }
}

Thank You

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.