- Home /
How to make a list store and save values even while changing scenes.
Skip directly to the question if you want
INTRODUCTION->
Hi everyone, gonna start with a couple of details first: my game is based around a TABS mechanic, by which I mean that the game is managed by the player in every aspect thanks to different windows, which he can access by clicking on different little buttons on top of the gameview (map, objectives, inventory). Each TAB is actually a different game scene. That, as you can probably guess, requires a lot static variables, which I've used so far quite efficently, allowing me to create seamless transitions between the sections (TABS), which are thus constantly updated. Consequently, thanks to this system, the typical scenario sees the player picking up an object in the ACTIVE TAB/scene and, if he moves to the INVENTORY TAB/scene, finding it in the inventory already.
THE PROBLEM->
The problem is that I've implemented the inventory system using Lists, of which I know very little. Basically, my inventory List (called itemList) contains lots of items of the custom class Item(string to show the name of the object, Sprite to represent it in the scroll-list, int to display the quantity). Everything works fine, in theory, because, as I populate the list adding items (through the inspector or via script), they do show up in the inventory scroll-list in the game (and in the inspector, for that matter). The problem comes when I move to any other TAB (say the MAP TAB, for example) and come back: the list presents only the one item I've put into it manually, via the inspector, as a test. It is basically restored from any addition (or subtraction, for that matter) achieved through scripting at runtime. I've tried using something like public static List but to no effect. I keep getting error messages like:
"Static member `CreateScrollList.itemList' cannot be accessed with an instance reference, qualify it with a type name instead" or
"Object reference not set to an instance of an object"
THE QUESTION->
Is there a way to make the List "static-like", so that it can store changes to its contents even when changing scene?
Answer by meat5000 · Oct 04, 2015 at 06:18 PM
I would say that in this case you may need an additive loading approach.
http://docs.unity3d.com/ScriptReference/Application.LoadLevelAsync.html
This way you can load and unload scenes into a currently existing scene.
Interesting, didn't even know such a thing existed! So what you'd be suggesting is for me to basically open the inventory as the base/root scene and then open all the other windows later, with this asynchronous method, thus not requiring for the list to save the values inbetween scenes, as technically I'd never be leaving the inventory scene (which would otherwise reset the list). Very clever! Thank you!! But wouldn't it be possible to do what I had intended at the very start? Is it something that Lists allow? (Oh, to store values inbetween scenes, that is...)
edit *
come to think of it double thank you as well, because I might be using this to enable the player to walk into different rooms in the same building during the active game without them resetting every time!!
I suggest this approach as it will allow you to keep using your List WHILST loading another scene.
Other approaches would include DontDestroyOnLoad or making static objects, both of which will retain objects, but I'm not sure if it retains data. Of course, during a scene change you get a hard load. Not a good approach for a seamless tabs system.
Give thanks with an upvote ;) If you try the method an it works, click the tick to accept the answer.
And, you are most welcome.
Your answer
Follow this Question
Related Questions
Is there any way to edit variables inside other scripts without declaring them as static? 1 Answer
Prefab's Script affecting all the Prefab 1 Answer
How can I preserve static object/data between editor and play? 2 Answers
Creating a list using a list from another script? 1 Answer
Instantiating a Coroutine? 2 Answers