2

I'm working on a save and load system for my game in Unity3D. The basics like position is simple, I just have

 public void Save() {

     PlayerPrefs.SetFloat ("X", transform.position.x);
     PlayerPrefs.SetFloat ("Y", transform.position.y);
     PlayerPrefs.SetFloat ("Z", transform.position.z);

 }

 public void Load() {

     float x = PlayerPrefs.GetFloat ("X");
     float y = PlayerPrefs.GetFloat ("Y");
     float z = PlayerPrefs.GetFloat ("Z");

     transform.position = new Vector3 (x, y, z);

 }

But then comes the part when I want to save custom variables. Here's how the class that contains them looks like:

     [SerializeField] private Stat health; 
     public static Stat Health { get; set; }
     [SerializeField] private Stat exp;
     public static Stat Exp { get; set; }
     [SerializeField] private Stat oxygen;
     public static Stat Oxygen { get; set; }
     [SerializeField] private Stat playerLevel;
     public static Stat PlayerLevel { get; set; }

So my question is, how do I get values of those stats in my SaveLoad script?

Fattie
  • 27,874
  • 70
  • 431
  • 719
Nech
  • 359
  • 3
  • 15
  • What do you mean by "save"? Save where? – Eduard Malakhov Nov 19 '16 at 15:57
  • 1
    This has been asked and answered so many times. Can only find one I have answered. Remove the `{ get; set; }` from it then follow [this](http://stackoverflow.com/a/40097623/3785314) – Programmer Nov 19 '16 at 15:59
  • 1
    Exactly, this is an ultra-duplicate. Do what programmer says. – Fattie Nov 19 '16 at 16:00
  • @nech click to HERE: http://stackoverflow.com/a/40097623/294884 http://stackoverflow.com/a/40097623/294884 http://stackoverflow.com/a/40097623/294884 – Fattie Nov 19 '16 at 17:17

1 Answers1

5

Just to drive home the point

1) In Unity

Totally forget about Serialization. It's that simple.

I can't even be bothered discussing why. It's just how it is!

2) Generally these days in games/mobile you should

Work with JSON

End of story. Fortunately it is incredibly easy.

3) To convert back and fore to JSON,

Do exactly what @Programmer says.

https://stackoverflow.com/a/40097623/294884

4) Finally to save that, honestly there's nothing wrong with:

Simply use PlayerPrefs to save the long JSON string. No worries.

Note that if you DO want to simply save it as a text file, which is fine, it is exceedingly easy to save it as a text file:

Just do this: https://stackoverflow.com/a/35941579/294884

(Notte: never use "streamwriter" in Unity for any reason. It's totally trivial to save/read text files.)

Repeat. Do >> NOT << touch, think about, or use "serialization" in Unity! For any reason, ever!

Enjoy life!

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • 1
    I agree and I never could have emphasized better how bad serialization is in unity! – Bizhan Nov 19 '16 at 16:30
  • If you're referring to [SerializableField] it's just for the purpose of accessing it from the Unity inspector without making it public. I'm trying to save the data using PlayerPrefs, but it doesn't recognize the Stat variable, which is a custom variable. Is there any way to use PlayerPrefs to use it? If not, how am I to use PlayerPrefs to save it into Json? – Nech Nov 19 '16 at 16:41
  • note, generally just use "public" rather than [SerializeField]. – Fattie Nov 19 '16 at 17:16
  • @nech Programmer explains how to use Json + PlayerPrefs in total detail here: http://stackoverflow.com/a/40097623/294884 .. as it says about ten times above :) – Fattie Nov 19 '16 at 17:17
  • My problem turned out to be in a completely different place, instead of referencing for example Health, I should've referenced Health.CurrentVal, and now it works, thanks though. – Nech Nov 19 '16 at 17:25