- Home /
The question is answered, right answer was accepted
Carry Object over multiple scenes - Adventure Game
Hello everyone,
I try to make an adventure game like the good old LucasArts games.
I have got the movement for my character, but now I don't know how to continue: My character should obtain a tagged gameobject (e.g. "Object1") with OnCollision2D and should be able to carry this object over multiple scenes until the player presses the M key. Then the character should release this object (the item should spawn next to him).
The problem is it's the first time I try to make this and I have no clue. If anyone has an idea or a script, I would be very grateful.
Thanks.
Answer by TanselAltinel · Nov 05, 2017 at 07:12 PM
Hi Porcus,
What you need is DontDestroyOnLoad
Then you can enable or disable the same object as long as it is needed. You don't need to spawn it as it will be there all the time, but you can deactivate it whenever you don't need it (or don't want to be seen on the screen).
Here's the documentation on the code: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
And here's a nice official documentation about Data Persistence: https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data
You would also need some way to make sure to keep the object where you left it when you leave the scene and then return back to it. I would suggest using Player Prefs to save a bool that says this object is in this scene. Then when entering the scene you check if the object is in the scene and if it is you load in the coordinates of that object (also saved using PlayerPrefs). You could also try keeping a note of which scene the object is left in, and keep using DontDestroyOnLoad, but when the object isn't supposed to be in the scene it's deactivated so it can't be scene and when you return to that place it's is activated again. (But it is never destroyed)
Follow this Question
Related Questions
Player Smoothness across network (PUN) 0 Answers
Unity 5 Crashing After Opening Project 1 Answer
Shader to dilate a binary image 2 Answers
Mesh Collider didn't work unless i walk slowly towards it 1 Answer