I am trying to make a puzzle game and set up my game to have a hub world and 4 different rooms to collect puzzles/interact with stuff. I am very new to unity and I set up a scene switch button going to the start of one scene
Inside Hub there is a button goes to “Door 1” scene
Inside Door 1 is a button that does to “Hub” scene
I have objects that can be moved in the door scenes, but when I press play and move from Hub → Door 1 → Hub → Door 1, stuff from the Door 1 scene has been reset. Is there a way to make it so the object stays in the same place/a puzzle will still be solved/people have already been talked to when going to that scene? I tried Object.DontDestoryOnLoad, but it keeps it on every scene. I just need it to stay in the same state as before when going back to that scene since everything will be localized in that area.
You need a way to observe, maintain and restore the state of things in your scenes during the duration of the game.
The easiest way to do this is with a GameManager. This can be as simple or as complex as you like.
ULTRA-simple static solution to a GameManager:
OR for a more-complex “lives as a MonoBehaviour” solution…
Simple Singleton (UnitySingleton):
Some super-simple Singleton examples to take and modify:
Simple Unity3D Singleton (no predefined data):
Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:
These are pure-code solutions, do not put anything into any scene, just access it via .Instance!
If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:
public void DestroyThyself()
{
Destroy(gameObject);
Instance = null; // because destroy doesn't happen until end of frame
}