Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

How to enable player to choose default scene to load 0 Answers

Saveing and Loading Problem 0 Answers

Saving doesn't work after Build 0 Answers

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges