Static class not working properly on game build

I’m making a rpg-like game and I’m using a static class to carry the payer’s basic information between scenes. Before loading a new scene my GameManager class saves the inventory and the player’s HP on the static class, then on the new scene on it’s Start function it loads the inventory and the HP. If I try it on the editor everything works fine, but when I build the game and try it out only the inventory is saved between the scenes, wich is weird because they are both handled by the same function.

Hi g-s-100, Static classes in Unity work a bit differently than they would in C# normally. The reason why is complicated, but should you want to define a propper static class this is how you do it in Unity.

public class GameManager : MonoBehaviour
{
	//Static instance of GameManager which allows it to be accessed by any other script.
    public static GameManager instance = null;              
    
	/* Safe player info here */
	public int score;
	public string playerName;
	public int level;
	...
	
    //Awake is always called before any Start functions
    void Awake()
    {
        //Check if instance already exists
        if (instance == null)
            
            //if not, set instance to this
            instance = this;
        
        //If instance already exists and it's not this:
        else if (instance != this)
            
            //Then destroy this. This enforces our singleton pattern, 
			// meaning there can only ever be one instance of a GameManager.
            Destroy(gameObject);    
        
        //Sets this to not be destroyed when reloading scene / Switching scenes
        DontDestroyOnLoad(gameObject); // VERY IMPORTANT
        
    }		
}

Now the most important part. This class needs to actually exist in game. It needs to be attached to an in-game object. Not having this will result in your class not working as Static.

I don’t know if it is defined how static members behave in the Editor. If the class is not unloaded when leaving Play mode, the assigned values might survive when switching from Edit to Play mode again.


An alternative you could check out is to keep the player information in a script attached to a game object that is shared between scenes.

The Object.DontDestroyOnLoad() method allows you to keep the (game) object when loading new scenes.

I created a singleton class with don’t destroy on load, but the problem remains. It works fine on the unity player, the data is saved between scenes and the singleton isn’t destroyed on load but when I build the game only the inventory is saved between the scenes, the HP reset to the default value. I don’t know if it’s important but, the inventory is an Item array (Item being a custom class) and the hp is a float array
.

Thanks for your help, but actually the issue was somewhere else. After Loading the scene I couldn’t acess the player’s change health function (where i get the value from the static class). I fixed it with a coroutine so the function would execute properly on the next frame. I’m not sur if it’s the best method to fix this but it seems to work properly now.