- Home /
Maintaining more than one player character objects between scenes
2 part question stemming from the same problem.
I am converting a game I started in Construct 2 to Unity. There is an overworld scene with a teeny sprite. Then each level goes to a side scrolling scene from the overworld. And before each level you can pick 1 of 4 characters to play as.
First off, I'm not sure the best way to store information tied to each character (health, ammo, etc). Unity seems to store the data on the actual object which would be great if I could just add "DontDestroyOnLoad" and let the same character with the same health and ammo continue from the last level. But I imagine I have to instantiate the selected character at the beginning of each level and let them get destroyed at the end.
The only solution I can think of is make an empty game object for each character. Store all the needed variables on that object. Add "DontDestroyOnLoad" to their scripts. Then before each level, transfer all the data from the empty game object to the instantiated Character Object. Then at the end of the scene, transfer the updated data back to the empty game object (spent ammo and lost health etc). Or is there a simpler way to keep all the data on each character between the scenes they aren't used?
Also I am having a hard time getting general functions happening. Because the characters are all instantiated, it's hard to target other objects. Like the health bar reacting to player taking damage. I end up tagging everything and then use "FindGameObjectWithTag". But by the end of this game there will be hundreds of tags ... is that typical? Seems a bit clunky and hard to maintain.
Any advice on how to approach this problem would be appreciated.
Unfortunately after a bit of research this link is the best I can find;
http://t-machine.org/index.php/2015/03/15/some-thoughts-on-loading-scenes-in-unity3d/comment-page-1/
It's a long article that says basically there is no good way to handle the problem (or any other situation using Unity for complex multi scene projects).
Still looking into a solution. I have CharacterSheet script for each of the 4 characters that holds all the variables used for each character. It's a convoluted mess but it seems to work.
Answer by fogsight · Aug 08, 2016 at 02:49 PM
@DTE462, You don't have to be confined to Unity's API. You can create your own Class to store all of the player's data and make it static, or instantiate it on some "DontDestroyOnLoad" script object to keep it alive. You can also move objects between scenes: http://docs.unity3d.com/540/Documentation/ScriptReference/SceneManagement.SceneManager.MoveGameObjectToScene.html
To keep track of objects you should use object pools (List or Dictionary etc. depending on usage, containing custom classes with targeting data for extra convenience). Finding objects on the fly is very costly, you should always reference them. Additionally creating dedicated targeting pools like "ObjectsThatInRange" would speed things up. As well as try reusing objects in pools without destroying them for additional performance gains.
Great! All this gives me some avenues to research. I appreciate it.
Your answer
Follow this Question
Related Questions
How can I calculate the "total length" of a series of instantiated gameobjects? 1 Answer
Instantiated prefabs acting different than those dragged onto scene 0 Answers
Storing XML data for multiple users 0 Answers
What is the best way to create and store levels in Android game. 0 Answers
sqlite encryption question 1 Answer