Reputation: 136
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)
Upvotes: 2
Views: 12237
Reputation: 613
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
Upvotes: 3