• 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
Question by Gamebient · Jan 09, 2022 at 03:17 PM · sceneassetsaveloadfile

Saving and loading scene specific data - (safe for scene renaming)

So here's the problem i encountered: Think about saving data between scenes. Most people would recommend some kind of Singleton-like approach. I'm fine with that but let's think about the following scenario:

  • I have a scene with a few things that should persist between scene changes

  • I save them in a list, containing their positions, other data and the name/buildindex of the scene they are in

  • I switch scenes back and forth

  • When I want to exit the game I store this list in a file, no problem here

  • Let's say I don't like the name or the buildindex I assigned to that scene and therefore change one/both of them. Then I make a new build of my game.

  • Now the data I saved points to a scene that does not exist anymore. In other words: I "lost" in-game progress

I've been thinking a lot about how to go about it but there doesn't seem to be a good way of storing the data safely. Do I really have to set one name and make sure to never change it?

Comment

People who like this

0 Show 0
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

Answer by Brodal · Jan 09, 2022 at 07:01 PM

I don't really understand the problem here. If things need to persist between scenes, you can mark them as dont destroy on load. Which will cause them to live in their own scene where all the other stuff that is marked as dont destroy lives.

The singleton like approach is pretty common, but singletons have a couple of problems. I usually go for a ServiceLocator/Provider pattern to solve this problem, with objects that need to be accessible from anywhere, singleton style. That way you only have 1 singleton, which is the ServiceProvider, which is also responsible for the lifetime of all services that are started. Here's a tutorial for one implementation of the pattern. https://www.c-sharpcorner.com/UploadFile/dacca2/service-locator-design-pattern/

To solve the problem with names changing, where you use the name of a scene as an identifier, i would look towards using something that doesnt change as an identifier. Either by using something generated by unity that you know doesn't change ( for example the guid of an asset in editor ). For self generated non changing IDs i usually go for System.Guid.

It feels like you're explaining two separate problems at once. The singleton/Service stuff is to solve the issue where you need classes that have an application wide lifetime, and are accessible from anywhere. Whereas the second problem is serialization of data to drive and how to handle persistence between game sessions.

Singletons don't store data safely, they just make your working memory accessible from anywhere in the code.

Comment

People who like this

0 Show 0 · 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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

162 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

Related Questions

How StreamedSceneAssetBundle differ from AssetBundle in terms of scripting? 0 Answers

Unity Crashes Startup/Saving Scene 0 Answers

Saving GameObject to file 2 Answers

Checkpoint autosave Method? 0 Answers

Sharing Violation On Path 1 Answer


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