- Home /
Custom class does not get serialized
Hello.
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.
Cookbookmanager:
public class CookbookManager: MonoBehaviour
{
private static int _maxRecipes = 16;
[SerializeField]
public CustomRecipe _TEST_RECIPE;
void Start ()
{
//_TEST_RECIPE = new CustomRecipe();
//_TEST_RECIPE.Name = "some name";
Debug.Log("Test-Recipe-Name: " + _TEST_RECIPE.Name);
}
}
CustomRecipe:
[System.Serializable]
public class CustomRecipe
{
private const int _maxIngredients = 6;
[SerializeField]
private string _name;
[SerializeField]
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?
Edit: The CookbookManager has a partent GameObject which is a singleton and which uses DontDestroyOnLoad(). Can this cause the trouble with the serialization?
I am not realy sure but those are private fields in CustomRecipe and if you dont safe the values you changed in the inspector in the prefab then it will always start with a null value!
Do you want to have a standart value, that is also displayed in the editor am i right?
The private fields are marked as SerializeField and therefor should be serialized. But I also tried using public fields, it did not work either.
I'm not using the inspector. Values are only altered via script.
Answer by Xarbrough · Sep 29, 2015 at 05:36 PM
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.
You are right, too much program$$anonymous$$g today. I put my data in a ScriptableObject and now its working as expected. Thanks
Ha, just assumed that @gyrosp was using inspector here with an object in the scene active. Looking back at the edits I can see this now. Excellent.
Answer by Kyle_WG · Sep 29, 2015 at 12:42 PM
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.
public CustomRecipe _TEST_RECIPE = new CustomRecipe();
unfortunately does not change anything. Both classes are already in two separate files. Any other idea :)?
Ack. Try a fresh instance of Unity with just these classes in. Also try removing static for now.
You don't have to / shouldn't create serialized custom classes manually. Those are created automatically by the Inspector.
The code in the OP won't work since he created an instance manually at runtime. What you do at runtime isn't reflected by the assets as Unity doesn't serialize anything at runtime.
So either create them "manually" from an editor script at edit time, or simply use the inspector to setup your object data.
Yeah, @Xarbrough pointed out in the marked answer that he was doing it runtime. I overlooked that before @gyrosp's edits as assumed he was trying to see the changes in the inspector from a GameObject in the Hierarchy haha. :D