2

I have a Problem with Using ScriptableObjects as a saving object within of my Unity Application. When I try to write values into them it all works quite fine, but if I want to close the application and Load the values of the ScriptableObject those are resetted to it's last values. Which destroys my idea of using them to save Game values.

I tried to debug the whole progress of saving Values to one of my Scriptableobjects this seemed to work and whenever I paused the scene the values where all within my Scriptableobject. But whenever I not paused the scene the Scriptableobject would not take any of the Values as it seems (here a little bit of example code about this)

[CreateAssetMenu(fileName = "stats1", menuName = "ScriptableObjects/Values", order = 1)]
public class ValuesSo : ScriptableObject
{
    public string Name;
}
public static class LifeValues
{
    public static string Name;
}
public class MainGameMB: MonoBehaviour
{
    //I drag and drop the Scriptableobject here so I have it referenced
    public ValuesSo Savedstats1;
    void Start()
    {
           Savedstats1.Name = LifeValues.Name;
    }
    //this is called by a Button call
    public void save()
    {
          LifeValues.Name= Savedstats1.Name;
    }
}

(this is only a bit of example code and should be everything that is intresting for the topic. If you have any questions or need a bit more background just ask and I can try to provide that)

Martin Peck
  • 11,440
  • 2
  • 42
  • 69
Assasin Bot
  • 136
  • 1
  • 1
  • 11
  • Scriptable objects should save any changes from run time to disk. In the code above you're never changing the value of `Savedstats1`, you're always using the static value set in `LifeValues`. Can you post `Save` and `Load` functions. – user14492 Nov 21 '19 at 13:06

1 Answers1

3

You need to introduce Serialization.
Save the files on disk before exiting the game (or at "save points") and load it on the beginning of the game (or when loading the save file).

Here's the link to Unity docs: https://docs.unity3d.com/Manual/JSONSerialization.html

Lyrca
  • 528
  • 2
  • 15
  • Thanks, I will try that. But all in all I was hoping to evade the Jsonserialization but this doesn't seem to work for now :/ – Assasin Bot Jul 20 '19 at 12:27
  • You could use `PlayerPrefs`, if it's something simple: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html – Lyrca Jul 20 '19 at 12:31
  • I think I am all fine with the Json but I still have one question how can I access the data after reopening the application because if I use the ```myObject = JsonUtility.FromJson(json);``` line I am still not sure wich object then "json" at the end of it should be or better sayed how I know where it is saved. – Assasin Bot Jul 20 '19 at 12:48
  • I figuered a way out I am now combining the JsonSerialization with the Playerprefs so that I can acces the Json string even after closing and reopening the game – Assasin Bot Jul 20 '19 at 13:03