I am developing for Android.In my game data is in objects which aren't derived from MonoBehaviour.So I am using binary serializer and taking references from the gameobject.It works, but I have too many variables to save most of variables are in script attached to gameobject Gamemanager So I made a prefab of gameobject of Gamemanager and saved it using prefabutility and load it again It works but Can someone tells me whether this is right approach or not, please??

  • \$\begingroup\$ You could serialize the objects you want to save in to a string and then store that with PlayerPrefs, which allows you then later load the data to a string and deserialize in to objects. \$\endgroup\$ – Lasse Jan 24 '17 at 13:13
  • \$\begingroup\$ I'm voting to close this question because it is a cross-post from Stack Overflow. Please choose the best Stack Exchange site and post your question only there. \$\endgroup\$ – Vaillancourt Jan 17 '18 at 20:28

This is going to work as long as you run your project from the editor. But when you try to actually build it, you will notice that it doesn't compile. The reason for that is that the class PrefabUtility is part of the UnityEditor packet, which doesn't get added to real builds of your game.

I ran into the same trap in my last project. I was lucky that I only used PrefabUtility in my map editor which I was going to dummy out of the release build anyway.

You will have to find a different solution to serialize your gamestate.

One possible alternative is the class JSONUtility which allows you to convert objects to JSON and back. It doesn't work on complete game objects, but it works with anything which extends MonoBehaviour or ScriptableObject and with any plain C# class when it has the [System.Serializable] attribute. Note that it will only serialize the public variables and properties of your classes.

  • \$\begingroup\$ Hey thanks for the answer, Can you please tell me What to use to save data for Android.I have too much data to save(many NPC-ENEMY stats) also it should work after updating I read on stack overflow that In Binarformatter when you update or change the class. Your old settings might be lost since the classes no longer match.So What should I use to save data JSON/XML? Also, do JSON/XML throw an error if I change class later? \$\endgroup\$ – paul p Jan 26 '17 at 4:48
  • \$\begingroup\$ @paulp This is a whole bunch of new questions which should be posted as new questions. \$\endgroup\$ – Philipp Jan 26 '17 at 9:08
  • \$\begingroup\$ okay I will post new question. \$\endgroup\$ – paul p Jan 26 '17 at 9:42

Use a ScriptableObject instead and mark the classes you want to serialize in it with [System.Serializable]. Mark each field you want to serialize into the .asset file with [SerializeField] or make it public.

When you've created the asset file, put it in the Resources folder and load it runtime with Resources.Load("assetPath"). Since it's an asset you should not have to Instantiate it once you load.

This handles serialization in the same way as a monobehaviour, but in an asset file on disk instead. It can handle arbitrarily much data up to a certain limit. If you have more than that, you should reorganize your data in multiple files instead.


Not the answer you're looking for? Browse other questions tagged or ask your own question.