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 /
avatar image
0
Question by FlyingRaven8011 · Feb 03, 2017 at 03:45 PM · c#unity 5load scene

loading gameobjects during scenechange

Hello everyone,

I am attempting to make a save and load feature for my game in unity 5.5. It works reasonably well already, I can save and load my data and when I am in the required scene everything is loaded correctly. But when I load from my main menu I prompt a scene changes after which I initialize my game objects, but when the game is done loading everything is empty.

I figure this happens because the game objects are made before the scene finishes loading and thus when the scene does change it goes into a empty scene because everything was loaded into the old one. Now what I don't know how ever is how to fix this problem.

This is my current code:

  public void LoadGame(int loadNumber){
     SceneManager.LoadScene("Scenes/Prologue");

     if (File.Exists(Application.persistentDataPath + "/playerInfo" + loadNumber + ".dat"))
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Open(Application.persistentDataPath + "/playerInfo" + loadNumber + ".dat", FileMode.Open);
         SaveGameData data = (SaveGameData)bf.Deserialize(file);
         file.Close();

         InitaliseGameComponents();

         var eventString = _GameProgress.Load(data.GameProgressData);
         var splitEventString = eventString.Split(':');
         if (data.DialogData != null)
         {
             _DialogControl.Load(data.DialogData, splitEventString[1], splitEventString[2]);
             _DialogDisplay.SetActive(true);
         }
         if (data.CombatData != null)
             _CombatControl.Load(data.CombatData);
     }
 }

Even though the SceneManager acts first the gameobjects that are being created in the _CombatControl are unloaded. Does anyone know a good fix for this? Or am I doing something fundamentally wrong?

Thank you in advance.

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
1
Best Answer

Answer by Hanoble · Feb 03, 2017 at 07:44 PM

Depending on your specific needs, you will either need to run the initialization of those objects after the scene load has happened, load the scene additively, or mark those objects as Don't Destroy on Load.

A common technique used would be to load a "Loading Level" from your MainMenu, for example a scene with a loading texture and some game tips or something, then load your game scene additively, initialize your saved state, and finally unload your loading scene (or leave it there and use it again as needed).

All three of those options should work, so choosing which one makes the most sense for your project and needs is the real question.

Comment
Add comment · Show 1 · 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
avatar image FlyingRaven8011 · Feb 04, 2017 at 01:51 PM 0
Share

Yes I was kind of airfreight that this was the only way to fix my problem. I hoped there was a other solution, but meh. I'll start restructuring the project now. Thank you for your replay.

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

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Load scene in unet behind a loading screen 1 Answer

A real head-scratcher 0 Answers

Object from list in scriptableobject in list 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