I am trying to make a simple video game and I need to use the “static” keyword for some reason
static public int Level;
but when I do that it doesn’t serialize a field for my variable. I tried to use this code but it didn’t work (I looked at the scripting reference of unity after testing it. It also says SerializeField cannot serialize static fields. )
[SerializeField()]
static public int Level;
Is there any way to serialize a static field?
Thanks for help from now.
You could create a helper field to store the value/ manipulate it in the editor and load it on Deserialization something like this.
public static int myStaticInt;
[SerializeField]
private int intSerializationHelper; //don't use for anything else
public void OnAfterDeserialize()
{
myStaticInt = intSerializationHelper;
}
public void OnBeforeSerialize()
{
//intSerializationHelper = myStaticInt; // uncomment this if you want the field to reflect the value of the static field on playmode
}
or probably better use another object (preferably a scriptable object to store the value), keep in mind that changes upon playmode will not remain after termination. This method is fairly unoptimized as it will perform the same action for every instance of said class. Don’t forget to add the ISerializationCallbackReceiver interface on the class.