Custom class does not get serialized


I have a class Cookbookmanager which ineherits from MonoBehaviour.
The Cookbookmanager has a class member CustomRecipe from a custom class which is marked as serializable.

Unfortunately this member does no get serialized, its values are always empty.


public class CookbookManager: MonoBehaviour 
	private static int _maxRecipes = 16;

	public CustomRecipe _TEST_RECIPE;

void Start () 
	//_TEST_RECIPE = new CustomRecipe();
	//_TEST_RECIPE.Name = "some name";
	Debug.Log("Test-Recipe-Name: " + _TEST_RECIPE.Name);



    public class CustomRecipe
    	private const int _maxIngredients = 6;
    	private string 		_name;
    	private string 		_description;

	public string Name
        set { _name = value; }
		get { return _name; }

I started the progamm without the comments of course and the Debug.Log showed the correct recipe name as expected. After that I outcommented the lines where I set the recipe but as it seemed the recipe was not serialized.

Any idea what is going wrong here?

The CookbookManager has a partent GameObject which is a singleton and which uses DontDestroyOnLoad(). Can this cause the trouble with the serialization?

Reading your comments, you are “not using the inspector”? So how are you saving your data then? Scriptable Object or Prefab? If not, where should the serialized data be saved to? Also, you actually need to create an object and assign your data, before you can save anything. So either you need to do that via the inspector, or via script and then save to an asset file.

That should technically work alright!? Weird.
Try separating those into 2 files and recompiling.

Also try (long shot):

public CustomRecipe _TEST_RECIPE = new CustomRecipe();

Just so it creates a fresh reference straight away.

Check for duplicates too.