Why does Unity use the default values for my Serializable objects while running?
I have a few classes that have the Serializable attribute and I'm able to edit them just fine. When I hit play, though, it uses the default values. Once I stop the game, the values change back to the ones I set them to. I've read in some places that people have been extending the Editor class, but it doesn't look like the Unity guys are doing that in this video: https://youtu.be/M4bH9lWOJE4?t=18m13s
I've spent a few hours looking for a solution (e.g. EditorUtility.SetDirty), but that seems much more complicated than what how the Unity guys made TANKS!.
Below is the code I have. To keep it relevant, I just removed the functions that I have. If you think the functions I have are relevant, let me know and I'll update the question :)
GameManager.cs:
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
public GameObject Camera;
public GameObject MessageCanvas;
public PlayerManager Player1;
void Start () {
if (Instance == null)
{
Instance = this;
}
else if (Instance != this)
{
Destroy(gameObject);
}
Player1 = new PlayerManager();
Player1.Setup();
}
}
PlayerManager.CS:
[Serializable]
public class PlayerManager
{
public GameBoard PlayerBoard;
public void Setup()
{
PlayerBoard = new GameBoard();
PlayerBoard.Initialize();
}
}
GameBoard.CS
[Serializable]
public class GameBoard
{
private Transform boardTransform;
public Grid Player;
public Grid Opponent;
public GameBoard()
{
Player = new Grid();
Opponent = new Grid();
}
public void Initialize()
{
Player.Initialize("Player Grid", boardTransform);
Player.Initialize("Opponent Grid", boardTransform);
}
}
Grid.CS
[Serializable]
public class Grid
{
public int Width = 10;
public int Height = 10;
public float TileSize = 1;
public Material TileMaterial;
public Color TileColor = Color.yellow;
[HideInInspector] public GameObject[][] Tiles;
[HideInInspector] public GameObject instance;
public Grid()
{
// Instansiate Tiles
}
public void Initialize(string name, Transform boardLocation)
{
// Initialize board here.
}
}
Before/After I hit play:
While the game is running:
Answer by Adam-Mechtley · Nov 21, 2016 at 10:42 AM
Hi @hydrosis!
Does your component have Start (), Awake (), OnEnable (), or ISerializationCallbackReceiver.OnAfterDeserialize ()? They may be relevant.
Alternatively, is anything accessing GameManager.Instance when the game begins and doing anything with these properties?
Does GameManager have a custom editor?
Thanks for the reply! I've modified my question to include the relevant functions.
Only my Game$$anonymous$$anager implements $$anonymous$$onoBehaviour, so that one has a Start() function.
$$anonymous$$y code currently isn't utilizing Instance, yet.
Game$$anonymous$$anager doesn't have a custom editor.
So the problem is that in your Start () method, you assign a new instance to the Player1 field, which completely erases the values that are deserialized for this field when the component is loaded. (Likewise, Player$$anonymous$$anager.Setup() reassigns the value for GameBoard, which assigns new Grids in its constructor.)
Basically, when Game$$anonymous$$anager.Start () is called, Player1 is already assigned and has read the serialized data for the instance, so there is no need to reassign it, to set up its fields or their fields, etc.
You might find this page in the docs helpful.
Your answer
![](https://koobas.hobune.stream/wayback/20220612101100im_/https://answers.unity.com/themes/thub/images/avi.jpg)