Question About DontDestroyOnLoad. Reset Variables on change Scene

Hello ! Sorry for my bad english first.

I have create a “private static Town_buy instance” like this. I want to keep my variable through all scene.
But when i create a “public int max_health” then the max_health will change when load new Scene. Its not happen with private varitable (Just Test).

Can you explain what really happen here. And How can i use public variable but also keep it when change Scene.

Thanks for Advice ! Pro :smiley:

A “static” variable is not bound to any instance of a class, it is shared among all of them.

So, while “private static Town_buy instance” is always present, “public int max_health” depends on each instance and is reset if the instnace is destroyed and a new one is created.

More on the static keyword: Static keyword in C# - GeeksforGeeks