- Home /
Keeping character's data through levels
Hello, I'm a begginer in Unity and I'd like to learn how to change a scene and how to keep my character's data in all scenes(like life remaining, etc.). I also would like to do a menu in the same way. I think it is a matter of design, and I would like to learn how to design it. Thank you and sorry about my english
Answer by McMutton · Jul 31, 2011 at 02:55 PM
You'd just pop a DontDestroyOnLoad onto whatever you'd like to maintain. http://unity3d.com/support/documentation/ScriptReference/Object.DontDestroyOnLoad.html
Answer by Peter G · Jul 31, 2011 at 03:00 PM
The easiest way is probably to use DontDestroyOnLoad()
. I would create a scene manager that is responsible for loading levels and managing player health and inventory and other similar things. Then tell Unity not to destroy it when the level changes.
//SomeOtherScript.js
static var gameManager : YourPlayerManager;
function Start () {
gameManager = FindObjectOfType(YourPlayerManager) as YourPlayerManager;
DontDestroyOnLoad(gameManager.gameObject);
}
//Now that object will not be destroyed when you load a new level and you can
//access it using SomeOtherScript.gameManager.
Your answer
![](https://koobas.hobune.stream/wayback/20220612163214im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do people carry over variables to other scenes? 1 Answer
level detector(scene) 1 Answer
Menu scene not loading correctly, doesn't show particles or animations and buttons behave strangely. 1 Answer
Splitting Android application and level loading 4 Answers
Selected object into next scene? 3 Answers