2
votes

I'm looking for a pattern to transition between scenes, moving one instance of an object to the next scene and then later returning it to the prior scene, but without destroying the other objects that were in the prior scene. Here is the context:

  • When my game loads, it connects to my server to get the list of characters the player can control and instantiates them dynamically from a prefab.
  • The loading script moves each character game object to the main scene and then loads that scene.
  • The main scene now has the characters, which can be interacted with. So far, so good. One of the commands is to pick a specific character and send them on a task, which has its own task scene.
  • I can move that character to the task scene and move them back when the task is completed. However, the other characters who were not on the task and should not be in the task scene are now destroyed because the main scene was unloaded.

I'm looking for a pattern to accomplish this. Most of what I've read suggests using DontDestroyOnLoad, but this actually moves all characters into all scenes, which results in too many characters in the task scene. Another option might be to create a game object that holds all the character information, pass that between scenes and have logic in each scene to re-instantiate the appropriate character[s] in that scene. This feels like a lot of overhead since I have no other reason to be wiping and recreating them constantly. Perhaps a third option is to restructure my game so the task scene is just added to the main scene and shows up as some kind of overlay that blocks/captures input. This sounds messy and likely to have performance issues (targeting Android).

Does anyone have a good pattern for this?

2
Do you need more than one instance of each character? Static declaration would do it, as long as you don't need 2 "archers" or whatever.Aaron
Yes, I need multiple characters, which is why I have this issue--moving one character around is comparatively simple. Each character is an explorer. You, the player, can hire them (resulting in more than one) and you can send them on adventures. Only one explorer can go on each adventure, and the other explorers need to remain in the main scene while they aren't adventuring.Justin Duewel-Zahniser
Static List<class> should work. As you create them, add them to the list.Aaron
And yes, I got that there were multiple characters. I assumed you had 1 class for each character type, but perhaps only 1 instance of each class (1 wizard, 1 archer, 1 thief, etc).Aaron
Ah, no, I have a class Explorer of which there could be n instances (probably up to a reasonable number such as 10--too many mouths to feed beyond that!).Justin Duewel-Zahniser

2 Answers

0
votes

with the scene manager you can create an empty scene, in this empty scene you put all of the objects you want to keep.

After you unload your first scene (not the one you create) and you load the next scene.

When your next scene is load you transfer all the object from the scene you create to the scene you just load.

PS : you have multiple scene load at the same time check this link for more informations : https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html

-2
votes

Turning comments into an answer.

Static variables can be accessed from any scene. Depending on the number of each class type you need, you can either instantiate a static class, or a static List/Array of class. Once you add your objects to it, you can access them from anywhere.

As long as this doesn't become a memory concern (thousands of large objects, eg), this should work well for your purposes (a few characters that need to persist throughout the game anyway).