• 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
7
Question by Agumen · Sep 08, 2013 at 03:38 AM · scenedata storage

What is the best way to pass data between scenes?

Hi, new to Unity here. I've worked through an entire book's worth of tutorials, but none answered my current problem.

I'm making a simple RPG. However, each town is / will be a different scene, and crucially the battle scene itself is a single scene that gets loaded for each battle on a simple battleground.

My question is: How do I keep the player's inventory, and team of heroes and their current stats 'alive' in data, and not deleted? I need all this data, pretty much all the time in every scene, but it seems too extreme to make it all static - I remember from C++ coding that using too many static variables hurts efficiency and is frowned upon. And by the time I'm finished these classes will have a LOT of components and variables.

It appears that using 'DontDestroyOnLoad()' on all of my crucial objects that I want to pass between scenes might work, but I'm not positive. Other answers I found on this site were inconclusive.

So a) How can I keep the data alive, and

b) Does it make sense to divide different towns into different levels like I'm doing, or is there a better way that lets me get around the data organisation problem.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by aldonaletto · Sep 08, 2013 at 05:20 AM

Statics don't hurt efficiency: they're simply stored in some special space created when the game starts, and survive until it finishes. The problem with static variables is more related to the fact that a static variable is unique for all instances - if you declare Player.health as static, for instance, all players will share the same Player.health, thus killing one player kills all of them at once. This makes it difficult to store in static variables info about objects that may have several instances in scene, but it's ok for single instance objects (the player of a single player game, for instance).

DontDestroyOnLoad offers a more flexible solution, but complicates development because you must not place in scene objects that are supposed to come from previous levels (you don't want two players in the second level of a single player game, for instance). You can use some development tricks, like instantiating the most important objects if they are not present at Start, but things may become complicated if there are lots of "important objects". A good compromise is to preserve only info objects - empty objects that hold the needed info in their scripts. You can for instance place the player in every level and make it search for its info object - if not found, create a default one.

Comment
Add comment · Show 3 · 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 Agumen · Sep 08, 2013 at 05:55 AM 0
Share

That's a good answer, thanks.

But what did you mean by 'you must not place in scene objects that are supposed to come from previous levels'?

If an object is not destroyed on load, it remains active, and potentially visible, right? That would be fine with me, I'd like my player to remain active and in the scene as he moves through levels.

Creating containers to move all the data back and forth from their proper objects would be a big hassle, I think. But thanks for explaining that possible solution, it's appreciated.

avatar image fafase · Sep 08, 2013 at 06:42 AM 0
Share

I guess he means that it gets difficult to efficiently maintain an object that survives scenes. Your player may not be able to perform some actions until later on in the game. But keeping the same object over scenes means he has all attributes possible from the start with loads of check to see if he is supposed to do or not. Even though some of them won't be used util level 100.

avatar image aldonaletto · Sep 08, 2013 at 05:11 PM 0
Share

Let's consider an example: you're developing Level 2, but the player is created only in Level 1. How do you test Level 2? Running the whole Level 1 just to test Level 2 doesn't seem a good idea (testing Level 100 would become a nightmare!). A possible solution is to have a control script in each level that checks for the player existence: if there's no player, instantiate it - this helps testing each level independently of the previous one.

The idea behind the "info object" approach is also to simplify development of higher levels. Since the player usually starts a new scene in a location different from where it ended the previous level, you must always move the player there at Start. You can do this in the control script mentioned above, or use the "dedicated info object" approach: the player itself is placed in each scene at the appropriate position, and the only thing that survives to scene changes is the info object (which may be instantiated when testing a new level).

What @fafase said also must be taken into account: different abilities or assets that the player should/could have in higher levels may be needed when testing these scenes. You may have to create or enable some of them during tests - but this is one of the many burdens related to game development, no matter which alternative you use.

avatar image
0

Answer by Sundar · Sep 08, 2013 at 04:00 AM

Have you tried Playerpref

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 Bob-The-Zealot · Jun 09, 2015 at 12:06 AM 0
Share

You understand that PlayerPrefs store the data on your computer, and you can only have one set of PlayerPrefs on one computer.

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

18 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

Related Questions

(PLEASE HELP) hi guys! i have a problem with my Score text 1 Answer

A bit of the scene is cut off in build 0 Answers

Duplicating Scene Level 1 results in _evel 1 when neeeds to be Level 2 1 Answer

Is there a way to activate a timer from another scene? 1 Answer

My life counter is not updating 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