Persistent State Across Scenes
Keep one state-machine owner alive across scene changes while treating scene objects as replaceable references that must be rebound after each load.
Watch the State Designer scene transitions tutorial on YouTube.
Build the ownership model
- Choose one persistent GameObject to own the State Machine.
- Mark that owner with the project’s persistent-object lifecycle, such as
DontDestroyOnLoad. - Ensure only one copy exists after loading another scene.
- Store durable values in Graph, GameObject, or Project scope according to their real lifetime.
- Rebind scene-local objects after the new scene loads instead of retaining destroyed references.
The imported Persistent Scenes A and B samples demonstrate one owner moving between two scenes. Add both scenes to the build list before testing the sample route.
How it runs
The active State and persistent variables remain with the owner. References to scene-only targets, cameras, UI, spawn points, or managers may become invalid when the old scene unloads. A scene-loaded Action, event, or project coordinator should supply the replacement references before Conditions use them.
Verify in Play Mode
Enter a distinctive State, change one persistent variable, and load the second scene. Confirm that there is still exactly one owner, the State and value remain, scene references point into the new scene, and returning to the first scene does not create a duplicate owner.
Troubleshooting
| Symptom | Check | Fix |
|---|---|---|
| Two machines run after a scene load. | Persistent owner plus a scene copy. | Use one creation authority and reject duplicates. |
| The machine persists but throws missing-reference errors. | Scene-local object references. | Rebind after scene load before evaluation resumes. |
| State resets on every scene. | Owner lifetime and initialization code. | Keep the same owner and avoid reinitializing an already restored machine. |