Keeping a variable from one scene to another

Hi,

I’ve been looking for a way to keep a variable attached to an object in a scene from disappearing when I load another scene. Is there a way to make the variable exist for the duration of the entire game regardless of what scene I load?

Thanks

Create a GameObject and attached script(s) just for the purpose of storing these persistent variables, and call DontDestroyOnLoad() on it.

Thank you very much.

Do make sure, though, that you keep track of which variables are held over, since if you go back to the original scene that first initialized those variables, they will still carry their current values and you have to be sure to properly reinitialize them to their startup values if the game is being restarted.