0
\$\begingroup\$

I have a test database of 37 records and I have stored each column into a list and I am storing each record into PlayerPrefs.

void storeStarData()
{
    // Code to store star data in PlayerPrefs...
    PlayerPrefs.SetInt("totStars", NumOfStars);
    int i = 0;
    while (i < NumOfStars)
    {
        PlayerPrefs.SetString(("starID" + i), StarIDID[i]);
        PlayerPrefs.SetString(("starName" + i), StarName[i]);
        PlayerPrefs.SetString(("starHIP" + i), StarIDHIP[i]);
        PlayerPrefs.SetString(("starHD" + i), StarIDHD[i]);
        PlayerPrefs.SetString(("starHR" + i), StarIDHR[i]);
        PlayerPrefs.SetString(("starGL" + i), StarIDGL[i]);
        PlayerPrefs.SetString(("starBF" + i), StarIDBF[i]);
        PlayerPrefs.SetString(("starRA" + i), StarRA[i]);
        PlayerPrefs.SetString(("starDec" + i), StarDec[i]);
        PlayerPrefs.SetString(("starMag" + i), StarMag[i]);
        PlayerPrefs.SetString(("starCI" + i), StarCI[i]);
        PlayerPrefs.Save();
        i++;
    }
    PlayerPrefs.Save();
    Debug.Log(PlayerPrefs.GetString("StarID0"));
    Debug.Log(PlayerPrefs.GetString("StarID1"));
    Debug.Log(PlayerPrefs.GetString("StarID2"));
    Debug.Log(PlayerPrefs.GetString("StarID3"));
    startSimulation();
}

First record in list StarIDID[0] = 1 Last record in list StarIDID[36] = 37

At the end, I've added 4 Debug.Logs that return the IDs of the first 4 stars. For some reason, only the first StarIDID is being stored into Player prefs.

Here is the output of the Debug.Log:

enter image description here

As you can see, StarID0 is the only PlayerPref with a valid value stored, the other ones don't have any values stored in them for some reason.

Is there a way to fix this and am I doing anything wrong here?

\$\endgroup\$
1
  • \$\begingroup\$ By the way: Saving a large number of tiny values as separate keys in PlayerPrefs is not very economic. The key names will take up far more space than the actual values. When your game has a lot of data to save, then it can be useful to serialize all that data into one string and then store it under one key. \$\endgroup\$
    – Philipp
    Commented Dec 20, 2019 at 13:56

1 Answer 1

1
\$\begingroup\$

You are setting the value for starID1 (lower case s) but you are getting the value for StarID1 (upper case S). Key names in PlayerPrefs are case sensitive, so these are not the same key.

You likely get a value for StarID0 because you set that value at some point in the past.

\$\endgroup\$
2
  • \$\begingroup\$ would it be more efficient to store and retrieve the values in a csv file? \$\endgroup\$
    – SidS
    Commented Dec 20, 2019 at 13:56
  • 1
    \$\begingroup\$ @SidS When you want to create a savegame system, then inventing your own file format is usually preferable to abusing PlayerPrefs for that. CSV can be a reasonable option for that. But keep in mind that this is not going to work if you export to platforms without a writeable filesystem (like WebGL). \$\endgroup\$
    – Philipp
    Commented Dec 20, 2019 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .