• 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 small502 · Jun 22, 2015 at 06:47 PM · scorelevel

Carrying a score from level to level

For a small project Ive been working on, Ive been trying to figure out how to have a persistent scoring system carry from level to level (ex: you must have made a score of X in level one for the level 2 button in the main menu to unlock). Im not really sure how to do this though. I know it has something to do with DontDestroyOnLoad() but I havent been able to figure out how. Thanks 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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by DiegoSLTS · Jun 22, 2015 at 07:32 PM

You have a few methods to do this:

  • Use DontDestroyOnLoad: You create an object in the scene and add a script to it. In that script you call the DontDestroyOnLoad function, which prevents this object from being destroyed when a new scene is loaded. Then in your scene you search the hierarchy to see if the object exists. The problem with this is that if you want to do quick tests you probably want the same object in the scene without the need to pass the previous level, so you might end with 2 objects that do the same. This all leads to using a Unity's Singleton (I don't like singletons): http://unitypatterns.com/singletons/

  • Use a static class: You create a static class that stores the values, and on each scene you check those values accessing static methods and variables. A static class can't be destroyed, so you'll keep your values while loading the new level.

  • Use PlayerPrefs or a file: This is similar to the static class, but instead of saving things in memory you save them somewhere else. It would be a requirement in the future anyway if you want to save the player's progress.

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
avatar image
1

Answer by TheXWolf · Jun 22, 2015 at 07:36 PM

Personally I would say some sort of encrypted or compressed save file, that way in the future the players' data is saved between levels. Personally though (depending on your type of game) you might consider using a single scene. If it's a high poly 3D game maybe not, but 2D games it works great. Scene's in unity can be huge so you might consider just moving to a different position in the scene and using 1 level in different pieces. Thay way nothing has to be moved and/or reloaded and only progress needs to be saved.

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
avatar image
1

Answer by gselfish · Jun 22, 2015 at 09:29 PM

You might want to check out this tutorial:

https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

It's very useful as it shows both how to use a singleton based on DontDestroyOnLoad, and how to properly save data between sessions.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

open door to next level, after 10000 points were added to your score (solved) 2 Answers

Score system Level Load 1 Answer

OnLevelWasLoaded script problem. 1 Answer

Score Question Java Script, problem 1 Answer

New score each level 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