- Home /
Handling scene objects
Salut !
Problem:
I have multiple unity scenes in my game. Each scene contains objects of various types, collectable items, bacgrounds, sprites etc. All this objects are statefull, meaning their attributes change, like, position is changing, sprite image to render etc.
Now, lets say I am on "scene1", I collect item, do all kind of things, I change the state of the whole scene. After that I leave "scene1", and go to "scene2".
Now, when I go back to "scene1" I want my scene1 to be in exactly the same state I left her, all the items in right positions with their attributes set...
Questions:
Do I basically want to create the whole scene from code every time it loads, because when I set up my scene through editor, each time the scene loads, it loads with all the objects in their originally defined editor states.
This leads to question: Should I even use editor to set up my scene objects ? Should I have all of my objects (ALL, including backgrounds and player) as prefabs so I can create all of them from code, avoiding the editor defined objects totally (for the release version) and using it only in development (to test things and similar).
Is there any way to make editor defined objects not to load in their original state but in their updated, actual state ? Because it is convenient to use editor to set up the scene nicely.
Are there any general good practices on this?
EDIT (Some more questions :P):
I could simply put DontDestroyOnLoad for all the objects and deactivate them when leaving scene :), and then activate them when I come back. Would this be an option for a smaller scale game with not so many objects ? Or this is absolute no ?
Should I serialize all objects on the scene so I can Instantiate them on scene load. Maybe I could use something like Memento pattern for that ? http://www.oodesign.com/memento-pattern.html
Answer by bradmarxmoosepi · Sep 24, 2014 at 11:37 AM
You could use the PlayerPrefs class for this.
Essentially, on Start
for your item:
void OnStart()
{
if (PlayerPrefs.GetInt("playerHasItem1") == 1)
{
// Do code for player has item
Destroy(gameObject); // Don't show the item if the player has picked it up already
}
else if (PlayerPrefs.GetInt("playerHasItem1") == 0)
{
// Do code for when the player does not have the item
}
}
You can use similar code in other places for checking if the player has the item. It would be nice if the PlayerPrefs
had a boolean storage function as well.
[EDIT]
For boolean storage in PlayerPrefs
, have a look at BoolPrefs from the Unify Community Wiki.
Thx for the answer, but I think PlayerPrefs is not designed to store so much scene related data. $$anonymous$$aybe it could be used to check which items player collected and similar, but no way I could put all the info for lets say 10 scenes, item positions and states for each scene, puzzle states, various stuff... I'm now looking into this memento pattern to build a system for storing and retrieving all that info for each scene