Search Unity

Question Looking for an way to save the game

Discussion in 'World Building' started by RubenVanOostveen, Sep 9, 2021.

  1. RubenVanOostveen

    RubenVanOostveen

    Joined:
    Jul 31, 2020
    Posts:
    91
    Alot of youtube tutorials i follow work no more or doesn't apply to my game. All of my videogame is working with turning off and on game objects / modeuals. Im looking basically for an save method to save the unity scene as is. for example:
    object.001 = true
    Car.012 = false
    exetera:
    when it loads back up the game it puts the correct things on and off like it was when the game ended.
    I've tried to look in which / what forum post it needs to be at but I think this is the best forum threat.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Actually, the Scripting forum would be the correct one.

    There is no shortcut for this that I'm aware of. You will need to iterate over your objects, and build a string representing all the state you need to save. You can store this string in PlayerPrefs. Then when you load, you'll check PlayerPrefs for that data, parse it out, and update the state of your objects.
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,695
    Here are some load/save notes:

    https://forum.unity.com/threads/save-system-questions.930366/#post-6087384

    Don't use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

    https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide

    When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls
    new
    to make one, it cannot make the native engine portion of the object.

    Instead you must first create the MonoBehaviour using AddComponent<T>() on a GameObject instance, or use ScriptableObject.CreateInstance<T>() to make your SO, then use the appropriate JSON "populate object" call to fill in its public fields.