Hello forum, I'm having difficulties to load properly a scriptable objet that I'm using for data storage. Everytime I try to load a game, it tells me: "InventairePerso must be instantiated using the ScriptableObject.CreateInstance method instead of new InventairePerso." How can I do it right? The load function is very simple : Code (CSharp): XmlSerializer leSerialXML = new XmlSerializer(typeof(Jeu)); FileStream file = File.Open(cheminDonnéesSauvegarde+"jeuxSauvés.sav", FileMode.Open); Jeu.JeuCourant=(Jeu)leSerialXML.Deserialize(file); //the warning message happens right here. file.Close(); The whole data structure is quite basic too (I'm only testing how it works now): Code (CSharp): [System.Serializable] public class InventairePerso : ScriptableObject { public int or; public string test; //a bogus inventory with a couple fields to see if it works } [System.Serializable] public class Jeu { public static Jeu JeuCourant = new Jeu(); //the whole data structure of the game in one static object public string nomDuJeu; //a string because why not public InventairePerso inventairePerso; //said bogus inventory as a subclass public Jeu() { inventairePerso = ScriptableObject.CreateInstance<InventairePerso>(); //properly instantiated the first time, no warning displayed at start inventairePerso.name = "case inventaire défaut"; inventairePerso.or = 12; inventairePerso.test = "inventaire vide"; nomDuJeu = "nom du jeu vide"; } }
ScriptableObjects aren't XML files. The error is telling you exactly what to do. Use ScriptableObject.CreateInstance: http://docs.unity3d.com/ScriptReference/ScriptableObject.CreateInstance.html
Thanks for the answer KelsoMRK, but could you explain me how to do it from a saved file? My concern is to write the whole game status to disk to load it in another run, and binary files do not seem fit for that - I couldn't write scriptable objects. I thought XML would be good because the structure of the object was correctly saved, but I can not instantiate it when I load the data... What's a better solution?
You can certainly save data to XML - or any other file format you want. ScriptableObjects aren't going to get you there by themselves though; and a lot of stuff in the UnityEngine namespace won't serialize straight-away. It sounds like you want to use your own data classes with custom inspectors that can read/write to files on the disk. That's fairly involved. For XML, I'd start with looking up System.Xml.Serialization on MSDN. Also note that 5.3 ships with some JSON serialization functionality baked in - so that might get you where you're going faster.
I'm sorry, I admit that I quite new to Unity (and C# alltogether), but I don't really understand... The scriptable object can be perfectly serialized into the XML file : all fields, classes and lists are there with the right tags, all classes and child classes fit nicely into each other (I complexified a bit the structure since last time)... so, the file is the exact depiction of the game world when it's saved. The only problem occurs during deserialization : I understand that scriptable objects must be instantiated and not created anew, and I don't know how to tell the deserialization method to do that...
So, I may have found an answer : somebody who had more or less the same problem (http://answers.unity3d.com/questions/766955/scriptableobjectxmlserializersad.html) decided to use ISerializationCallbackReceiver, cf: http://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.OnBeforeSerialize.html It seems possible to teach Unity to serialize and deserialize things the way we need them to be. If anyone is familiar with that, any further information would be greatly appreciated