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
1
Question by nnatindim514 · Jan 20, 2018 at 11:16 PM · scene-loadingloadinggamestate

How to keep game states while loading different scenes,How to keep game states while changing scenes

alt text In my game, the Player must collide with the object tagged "Next" in order to load the next level. Game state should = Level.LevelTwo and the Scene Manager will LoadScene(1). Once scene 1 (i.e. level 2) is loaded, however, the game state reverts back to Level.LevelOne. How do I go about and fix this issue.

P.S. currentLevel = Level.LevelOne is called at the Start function. Would this be part of the issue? Is the start function everytime each scene is loaded or reloaded? Thanks.

,

codehelp.png (14.3 kB)
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
7

Answer by Lysander · Jan 20, 2018 at 11:36 PM

When a new scene is loaded, all objects in the previous scene are destroyed, and all objects in the new scene are instantiated. In order to keep values from one scene to the next, there are a few different options.

1: Use additive scene loading. With this, the old scene isn't destroyed, but the new scene is added and loaded so both scenes can exist at the same time. You have to specially design your game around this concept though because you can't use drag-and-drop referencing for anything except objects/behaviours within the same scene. If you had a "player avatar" in 2 scenes, then there would be a duplicate when adding the second scene additively. I don't recommend trying this until you have a very good grasp of Unity and programming in general.

2: Use DontDestroyOnLoad(GameObject). This will make the GameObject survive changing scenes- this occurs by automatically (when the scene change is occurring) putting that GameObject into a special additive scene that is never unloaded. Similarly to additive loading, an object you set to DontDestroyOnLoad will be duplicated if you load a second scene that also contains that object- you have to be careful to either immediately destroy duplicates, or don't have the objects in multiple scenes to begin with. The latter case can make testing scenes in the editor complicated.

3: Use static fields. It's the object instances that are destroyed- static fields will survive just fine because they aren't tied to any object instances to begin with. You do have to be careful about your Awake and Start functions overwriting these values though, when the new instances are made for the next scene.

4: Use static classes. Static classes, like static fields, aren't tied to a specific object instance. You cannot make static MonoBehaviours (MonoBehaviours are by definition objects), but that's not any huge loss. You can assign the value to a field on a static class before changing scenes, and the retrieve the value from the same place when loading a new one.

5: Save the data to a file. PlayerPrefs are an easy solution for data that persists between scenes, but fair warning that it'll also persist between game sessions (quitting and restarting). This applies to other forms of game data saving as well- you can serialize an object and then write it to the local disk to load later. If you only want it to persist between scenes, just store the data right before the change, load it in the new scene, then destroy it (delete the key, in the case of PlayerPrefs).

I can't really tell you which method to use as I don't know the specific needs of your project, but some combination of these should help you with your data persistence.

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

77 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

Related Questions

How to select scene based on device screen size? 1 Answer

How do I use png image as a loading Scene in unity3d in C# 1 Answer

Can you load a level through using PlayerPrefs? 0 Answers

optimizing load scene 0 Answers

Load level on button press not working - new scene loads instantly without pressing key 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