0
\$\begingroup\$

My game has a bunch of GameObjects that I created with code at runtime and that I "store" in an array in a static class.

When I unload the scene and load it again, these objects are gone (of course). Is there a way to add the objects I store in the array back to the scene, so I don't have to create them again every time the player opens the options menu?

According to a bunch of old threads you weren't able to save a whole scene (including all of the scene objects and their locations) and restore it again later in Unity 5. Now that Unity 2017 is out, is that still the case or can you actually load previous scenes again (how would you do that)?

\$\endgroup\$
  • \$\begingroup\$ Do you mean something like DontDestroyOnLoad ? \$\endgroup\$ – TomTsagk Apr 16 '18 at 11:54
  • \$\begingroup\$ 1. You don't need to unload your scene when you want to show your options menu, so it sounds like the easiest solution is to simply not unload your scene in the first place. 2. There is nothing that would prevent you from writing down the properties of the objects in an array and, when you want to restore them, instantiating new ones with the same configurations. All the old threads say is that there's not a single built-in call for that, so you'd have to manage that serialization & deserialization yourself. \$\endgroup\$ – DMGregory Apr 16 '18 at 12:36
  • \$\begingroup\$ @TomTsagk No, I don't want to see the objects from my actual "game" scene in the options menu or even have access to them. Let's say that the player wants to see a simple coordinate system (3 colored lines). After creating it, I also save the 3 lines (1 GameObject each) in my static class. So when the player opens the options menu, changes a setting and closes it again, I want to restore the 3 GameObjects in the "game" scene. Reacreating them wouldn't be that bad because it's just 3 lines but with bigger objects, re-adding instead of re-creating would save a load of resources and be faster. \$\endgroup\$ – Neph Apr 16 '18 at 12:38
  • \$\begingroup\$ @DMGregory 1. The thing is that I don't want to see the current objects in the options menu and since the objects that are created can be small or big, I can't just teleport the player/camera (it's 1st person) to 1k/1k/1k because there's no guarantee that there won't be anything there unless I check every object for their coordinates first. So it's either that or re-adding the GameObjects I already created to the scene. 2. That's what I have the static class for. ;) Can you actually instantiate a GameObject again (as long as you store the "original" instance), even if it's not a prefab? \$\endgroup\$ – Neph Apr 16 '18 at 12:45
  • \$\begingroup\$ 1. Just use layers so your menu screen camera doesn't see any objects on your regular game layers — no positioning hacks required. 2. You can instantiate from any GameObject, but it actually has to exist in your scene or assets to do it. If you unloaded the scene then you don't have the original anymore — you just have a dangling managed handle whose underlying GameObject has been destroyed C++ side. Storing mere references won't suffice to bring them back from the dead. \$\endgroup\$ – DMGregory Apr 16 '18 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.