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
4
Question by LHP · Jan 31, 2010 at 05:33 AM · gameobjectscene

Retaining GameObject data when switching scenes?

I'm making an RPG, and wanted to know how I would be able to retain a GameObject's possibly changed data/variables (such as HP, coordinates, etc.) when switching scenes. I am asking this because when a new scene is loaded all of the previous scene's GameObject's (such as the character object in that scene) data/variables (HP, etc.) will be set to their defaults and all of that mandatory info would be lost.

Any help would be appreciated. Thank you!

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

4 Replies

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

Answer by Azound · Jan 31, 2010 at 06:01 AM

You could call Object.DontDestroyOnLoad(myGameObject); and then just keep the same gameObject from scene to scene.

Comment
Add comment · Show 2 · 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 scriptocalypse · Mar 29, 2011 at 09:35 PM 0
Share

But this will cause there to be two copies of the items in question when you return to the scene. You'd have the original object in the position you left it, and a carbon copy positioned right where it was in the authoring tool. That's not really helpful.

avatar image Azound · Apr 07, 2011 at 05:23 PM 0
Share

You can create the permanent objects in one scene, set them to not destroy on load, then load the scene you really want to play. Then you never go back to the loading scene, just to the scene you want to play again.

avatar image
1

Answer by Dec_bot · Oct 18, 2015 at 09:19 AM

You could do PlayerPrefs.SetInt("string", int); This basically allows you to store integers on the computer, and it would save this even after the game is closed. For example:

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
     public int gold;
     public int health;
 
     public void loadLevel () {
                  PlayerPrefs.SetInt("HP", health);
                  Playerprefs.SetInt("Money", gold);
     }
 }
 
 Then in the next level make it go:
 
 
 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
     public int gold;
     public int health;
     // Use this for initialization
     void Awake () {
                  health = PlayerPrefs.getInt("HP");
                  gold = PlayerPrefs.getInt("Money");
     }
 }

There's a lot more information in the documentation (just do Ctrl+" on PlayerPrefs in MonoDevelop).

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
0

Answer by rawsteel · Jan 31, 2010 at 11:12 AM

You could also consider creating a "checkpoint" before level changes where all Data are written e.g. in a Savegame/File/Database and reloaded (respectively only the data needed for the new level) after every level change. Thus you can purge the memory from data unneeded for the scene (e.g in a indoor scene you barely need cooridates of visited places or other data related to outdoor levels)

Comment
Add comment · Show 2 · 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 LHP · Jan 31, 2010 at 10:54 PM 0
Share

I was sort of thinking of the same idea, but I don't have the slightest clue on how to write data, let alone load it and read it, is there some place you could point me to for help on this?

avatar image rawsteel · Feb 01, 2010 at 09:11 AM 0
Share

I've seen commercial RPGs (e.g. Drakensang) using SQLite files for their savegames. They saved practically everything in there. Depending on your target platform you can use any embedded database, preferable SQlite since there is plenty of documentation and c# wrappers for it.

avatar image
0

Answer by Dwair · Jan 31, 2010 at 11:36 AM

Another approach (not the best but an easy one) would be making static objects which can store player data. Those objects don't need an instance to be altered, but be careful to reinitialize those objects appropiately.

With static objects, you can also avoid passing lots of data from one object to another, or keep looking for objects every scene like in DontDestroyOnLoad() or store data Methods, but player data will be always on memory, and most programmers consider using static objects a bad programming habit.

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 CarterG81 · Jan 30, 2014 at 10:10 PM 1
Share

$$anonymous$$ost stupid programmers consider using static objects as bad program$$anonymous$$g habits.

$$anonymous$$ake sure people know that there is absolutely nothing wrong with using static variables, and that the paranoia around them is mostly from idiots who have no idea what they're talking about.

I have seen it time and time again, would so-called programmers (even professionally hired programmers) say that static variables are "evil". These people are idiots, plain and simple. It is best to always ignore all their advice, as they pollute the internet with this sort of misinformation.

Static variables are only bad, if you make them bad- just like ANY other variable. If you want a variable to hold game data, such as a "player character", as long as there will only ever be ONE "player", it would be perfect to use a static variable. If you want to add a second player, then you need only make a second static variable, "Player2". As long as you know what 'static' means, there is no such thing as bad program$$anonymous$$g.

Likewise, the argument that a variable can be "bad" is as ludicrous as those who ignore the fact that ALL variables are "bad program$$anonymous$$g" if they're...bad program$$anonymous$$g. The idea that somehow 'static' is worse than any other misuse of a variable type, is purely idiots parroting what other idiots parrot, and the original know-nothing who came up with the idea. It is the equivalent of highschool gossip or people pretending to be extreme know-it-alls when they have absolutely no idea what they're talking about.

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

2 People are following this question.

avatar image avatar image

Related Questions

How Do You Have Multiple High Scores For 1 GameOverScene? 1 Answer

How to determine bounding box of scene? 2 Answers

Trouble while trying to manipulate object. 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Enable/Disable objects, with script 9 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