- Home /
Keeping variables through scenes
Hello, I am currently having this problem! (:
I have a page, 10 matches, and.. I think that's it, but when I go to the next scene, it doesn't have my information there... How do I make this happen is my question. (: Thank you!
Answer by Ashish Dwivedi · Jan 31, 2014 at 02:57 PM
The object that you want to keep persistent through scenes, you need to call "DontDestroyOnLoad (transform.gameObject)" this method once in awake or start by passing object reference.
I currently use this method so I support this answer.
The only problem this creates is that if you have dependency on these objects for a specific scene they will always need to be created.
You can get around this by storing all your global objects in a prefab and placing them in every scene. Then running a check on if these objects already exist or are active don't initialise any more of them.
Otherwise you will try and test your second scene and it won't work entirely due to dependency from objects created in another scene.
So... I have this on my FPS character... but it's still not working? Do I have to add multiple lines to each script?
Yes, you need to call this method once for all the objects which you want to persistent through scenes.
All right... I had this set up before... The DontDestroyOnLoad... But I'm still having the issue of missing variables..?
Can you paste your code here having missing variable?
Your answer
![](https://koobas.hobune.stream/wayback/20220613132205im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Scene Loading Crash 0 Answers
4.6.1f1: Can't save scene 2 Answers
Scene Saving In Between Runtimes 1 Answer
Unity will not allow me to export anything, or save any new scenes 1 Answer