Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to save without playerprefs?

Discussion in '2D' started by NeoEtIchiro, Apr 23, 2021.

  1. NeoEtIchiro

    NeoEtIchiro

    Joined:
    Mar 7, 2021
    Posts:
    6
    Hello, I am creating a 2D platform game and I have added a coin collecting system. I would like that when you collect a coin, if you finish the level and come back to it, the coin is not there anymore. By default unity reset a scene when we leave it, I can't use playerprefs because I can't create one per room, do you have a solution to my problem?
     
  2. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    757
  3. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    one potential way to do saves without playerprefs is to write text files using StreamWriter and read it back out using StreamReader (those are both basic c# System.IO classes)

    you can base the path you write your save data to on Application.persistentDataPath, this is where most smaller games save their save data and this path works on most systems

    furthermore you can serialize and deserialize data with json utility (https://docs.unity3d.com/Manual/JSONSerialization.html) with which you can convert an object of a class to a text file (ToJson()) and create from such a text file your object again (FromJson<class>())
    this only works for serializeable fields though (in other words, works well if the object is defined by numbers and text, wont work for most other things unless you write your own Serialize and Deserialize function)

    ---------------------------------

    though i gotta say, i dont see why you cant use player prefs... you can just create the keys dynamicly during runtime since they are just string:

    like PlayerPrefs.SetInt("RoomKey"+roomindex; theValueYouWannaSave)
     
  4. NeoEtIchiro

    NeoEtIchiro

    Joined:
    Mar 7, 2021
    Posts:
    6
    I made it with playerprefs, i found the solution thanks you.