• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Deeblock · May 04, 2018 at 03:57 PM · scripting problemsceneloadingsavingsaveload

Instantiating objects only after a scene is loaded

I'm trying to spawn in objects into the scene from a save file. All the logic is done through my "GameManager" script which is a singleton and persists across both scenes. When transitioning from my main menu to the main level, I call

 SceneManager.LoadScene("main_level");

However, this totally screws up my entire saving and loading methods. Apparently me methods get called before the scene loads, and hence the loadscene function resets my entire scene which throws a bunch of errors. How do I resolve this?

 public void LoadGame()
     {
         SceneManager.LoadScene("main_level");

         string jsonData;
         if (newGame) // Start new game
         {
             var defaultPath = Application.streamingAssetsPath + "/Default/Default.json";
             if (File.Exists(defaultPath))
             {
                 jsonData = File.ReadAllText(defaultPath);
                 gameData = JsonUtility.FromJson<GameData>(jsonData);
             }
             else
             {
                 Debug.LogError("Save file is missing at: " + defaultPath);
             }
         }
         else if (File.Exists(path + "/" + saveName))
         {
             jsonData = File.ReadAllText(path + "/" + saveName);
             gameData = JsonUtility.FromJson<GameData>(jsonData);
         }
         else
         {
             Debug.LogError("Save file is missing at: " + path + "/" + saveName);
         }
  
         /* Instantiate required objects */
  
         // Trees
         var treeParent = new GameObject("Trees");
         foreach (TreeSaveLoad.TreeData tree in gameData.trees)
         {
             var treeType = oakTree;
             switch (tree.type)
             {
                 case (TreeSaveLoad.TreeType.Redwood):
                     treeType = redwoodTree;
                     break;
                 case (TreeSaveLoad.TreeType.Birch):
                     treeType = birchTree;
                     break;
                 case (TreeSaveLoad.TreeType.Beech):
                     treeType = beechTree;
                     break;
                 case (TreeSaveLoad.TreeType.Pine):
                     treeType = pineTree;
                     break;
                 case (TreeSaveLoad.TreeType.Oak):
                     treeType = oakTree;
                     break;
                 case (TreeSaveLoad.TreeType.Maple):
                     treeType = mapleTree;
                     break;
                 default:
                     Debug.LogError("Missing tree type.");
                     break;
             }
             var treeChild = Instantiate(treeType, tree.position, Quaternion.identity) as Transform;
             treeChild.SetParent(treeParent.transform, true);
             yield return null;
         }
  
         // Call load event
         if (EventManager.Instance.e_loadGame != null)
         {
             EventManager.Instance.e_loadGame.Invoke();
         }
  
         // Call last, after loading is finished
         if (EventManager.Instance.e_loadedGame != null)
         {
             EventManager.Instance.e_loadedGame.Invoke();
         }

Here is the full code for reference. I call all my methods and events AFTER the loadscene call. This script exists on the GameManager object which persists from the main menu to the new scene.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by shadowpuppet · May 04, 2018 at 04:10 PM

maybe check to see if scene is loaded first https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

215 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the best way for loading ,editing and saving Levels in unity ? 1 Answer

Saveing and Loading Problem 0 Answers

Can't figure out how to serialize my save data... 1 Answer

Saving and Loading from different scene 0 Answers

Saving a Player Pref Exclusive to that instance of a script 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges