- Home /
Serialize array of custom class in custom class
Hi! I have a problem with my custom class not saving after closing unity/recompiling.
public class HeadClass {
var eyes : Eye[,];
}
public class Eye {
var m : Layers[];
}
public class Layers{
var listOfLayers : List<GameObject>;
}
Now, inside a script, the HeadClass class is saved:
@SerializeField
var allHeads : List.<HeadClass>;
But the remaining classes inside the HeadClass are not. I think they are not beeing serialized, but I dont know how to make this work. Can i serialize those classes somehow? EDIT: and i tried to use serialise (docs) on my custom class, but that also doesn't work:
@script Serializable
public class Eye {
var m : Layers[];
}
gives me an error.
Won't reply this as answer but as comment, since you are using Javascript and I do C#, but this is how I do it:
[System.Serializable] public class Conversation { public int ConversationId; public string[] $$anonymous$$essages; public bool AlreadyDisplayed = false; }
public class NPCBehavior : $$anonymous$$onoBehaviour { public List Conversations; }
Not sure what is the syntax for Javascript. I don't have to mark any property as Serializable, since whole class Is marked like that. If you want, you can "hide" properties from serialize with [System.NonSerialized] (again C#), but in this case, I'm serializing all the props.
Hope this helps
Hey spiceboy, thanks for info. I checked unity docs, and the [serialize] command, but cant use it in unityscript with syntax mentioned in those docs. Every time I try to serialize the main class the Eye class resets and is empty. Did a workaround: repopulate array after every recompile. It's a pain but it works.
Answer by hvilela · Oct 26, 2012 at 11:17 PM
To the class Eye be serialized, all this properties also need to be serializable and Layers are not.
yes, but how can I serialize the Eye class? Unity docs syntax doesn't work. Only way i can think of is to serialize the Eye class as a array of serializable type and use that array for later repopulation. But it is a mess.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082921im_/https://answers.unity.com/themes/thub/images/avi.jpg)