- Home /
FileStream/BinaryFormatter save static variables without PlayerPrefs
I followed Brackeys tutorial on Youtube to Save&Load files but i can not save static variables with this method because they can not be instantiated. I guess i could try to change every static variable to a nonstatic variable but i like to use static variables in general. Everything else works fine. Help would be appreciated!
Answer by andrew-lukasik · May 22, 2021 at 04:56 PM
Keep your runtime classes totally separate from serialization data classes and there will be no issue, for example:
runtime state classes:
public class MySceneController : MonoBehaviour
{
public static int Score = 0;
}
public class Player : MonoBehaviour
{
void Awake () {}
}
serialization data class (what is being saved):
[System.Serializable]
public class GameState
{
public int player_score;
public Vector3 player_position;
}
then:
var gameState = new GameState();
gameState.player_score = MySceneController.Score;
gameState.player_position = player.transform.position;
/* code that serializes and writes gameState to file */
Your answer
Follow this Question
Related Questions
Local alternative to static variables? 1 Answer
Does Quality Level remain between scenes? 1 Answer
My static variable is not changing 2 Answers
How do I make a script that will allow my character to shoot projectiles? 1 Answer
Call variable from other script but not allow it to be updated by that script 2 Answers