- Home /
changing scene vs. instantiated prefabs
In my game, there is a main scene where - to explain in simple terms - the user builds. This is done by prefabs being instantiated when there is a click detected.
Recently, I added another scene which is a shop were the user can purchase upgrades with in game money. There is a button in the top right of the screen which, when clicked, loads a separate shop scene, however when the user returns the instantiated prefabs that were previously placed no longer exist.
Is there a simple way to correct this? Or is it a more complex problem, I tried to 'google' a solution and searched the forums, but could not find anything. If I have been unclear about this please tell me and ill try to ask in more detail.
as @lktfl says eventually you'll have to do it properly and one way or another store all your data, constructions etc.
however, you should also be familiar with simply bringing up say a white panel and having your shop on that.
the answer to your question is there is no easy solution, like you can's "set aside" or something a working scene, so that's it.
Answer by lktfl · Apr 08, 2013 at 05:08 AM
There are two ways I figure you could go about doing this based on the information you've provided. There's probably an easier way on pro but since I don't have that here are my answers.
Hard Way (you wont have to redesign your scene/game:
For your problem, when the user clicks the button that loads the shop scene you need to store all of the objects positions, rotations, scale, and any states or variables they may have had. Then when the user goes back to the scene you need to put(instantiate) all those objects back using the data that was stored.
Easy Way (but you'd probably have to redesign your scene/game a little)
Instead of creating two separate scenes, why not just have everything (all gameObjects) in one scene? When the user clicks the button that loads the shop you could just deactivate all the gameObjects that you would have in your main scene and then activate all the gameObjects you would have in your shop scene. This is much faster than loading a new scene. Now when the user clicks the button to go back to the main scene you just deactivate all the gameObjects for the shop, reactivate all the gameObjects for your main scene and Voilà! all your gameObjects are still there, in the same place and state you left them.
Oh, and each time you change between main or shop you should set the objects being deactivated to static and the ones being activated to non-static(unless you want an object static all the time of course).
Personally I'd recommend doing both of these because the hard way is extremely reliable and the easy way is extremely fast. What I mean by this is you do it the easy way while having a script that stores all the position, rotation, scale, etc. of the objects that the user puts down. This is a safety just in case anything happens or anything goes wrong. And its good just to store the all the object's data if you have a game where the user saves and reloads levels (again i don't know anything about your game)
Sorry, I'm not that good at explaining stuff but I think you understand what I mean by all of this... hopeful :)
$$anonymous$$y Unity Serializer might help with the storage of the designed levels. I would also recommend just switching cameras to reveal the shop scene.
as stated by whydoidoit, use multiple cameras, and ins$$anonymous$$d of disabling/enabling the renderer of all your objects, simply put them on separate layers (shop and non-shop), then have the cameras Layer$$anonymous$$ask : use the Culling $$anonymous$$ask feature to show the shop objects with one camera, and non-shop objects on the other camera.
Thanks! I think the best way may be to have a GUI box pop out in front of the main scene, this way I don't have to change scene or deactivate anything. Thanks!