0
\$\begingroup\$

This is the code...

void storeStarData()
{
    // Code to store star data in PlayerPrefs...
    Debug.Log("Writing to PlayerPrefs");
    PlayerPrefs.SetInt("totStars", NumOfStars);
    int i = 0;
    Debug.Log("test");
    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)]);
        Debug.Log("star " + i + " complete");
        i++;
    }
    PlayerPrefs.Save();

    i = 0;
    int ok = 0;
    while (i < NumOfStars)
    {
        if (PlayerPrefs.GetString("starID" + i) != null)
        {
            ok++;
        }
        i++;
    }
    Debug.Log(ok);
}

And this is the Debug.Log output that I am getting...

Debug.Log output

As you can see, the program gets up to the First Debug Log Debug.Log("Writing to PlayerPrefs"); But then stops before the Debug.Log("test");.

After removing PlayerPrefs.SetInt("totStars", NumOfStars); The program gets to the Debug.Log("test");. So it is the PlayerPrefs.SetInt causing the issue.

How do I fix this?

\$\endgroup\$
  • \$\begingroup\$ What does contain NumOfStar? \$\endgroup\$ – Vaillancourt Dec 16 '19 at 22:40
  • \$\begingroup\$ Also, what do you mean by "stops"? The program crashes? It exits the function? It freezes? \$\endgroup\$ – Vaillancourt Dec 16 '19 at 22:41
  • \$\begingroup\$ NumOfStars is an integer that contains the number 36. The editor doesn't freeze or hang. The program just doesn't execute anything after that \$\endgroup\$ – SidS Dec 16 '19 at 22:43
  • \$\begingroup\$ Sounds like there may be an error interrupting the execution of this routine. Can you check your console for warnings or errors, in case you might have toggled those off? \$\endgroup\$ – DMGregory Dec 16 '19 at 23:44
  • 1
    \$\begingroup\$ Oh. Yeah, most of the Unity API methods are main thread only. So are the errors that get logged to the console. What most likely happened is you tried to use PlayerPrefs off the main thread, it threw an exception saying "this must be run on the main thread" but you didn't catch and print that exception so instead the thread was killed silently. This is why it's important to include context in your question - if we could see this was being run in a thread we could have solved it for you right away. Please post your solution as an Answer! \$\endgroup\$ – DMGregory Dec 18 '19 at 18:04
0
\$\begingroup\$

Turns out Unity doesn't play very well with System.Threading as I was running this function off the main thread to prevent the GUI from freezing (I am dealing with almost 120,000 records in the list).

Once I ran the function on the main thread, all the problems disappeared and PlayerPrefs started working normally.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ For saving on this magnitude, you might want to consider using System.IO methods to write the data to your own file. That can happen on its own thread so it doesn't impact your framerate. \$\endgroup\$ – DMGregory Dec 18 '19 at 18:54
  • \$\begingroup\$ Moving away from PlayerPrefs and using a Singleton script to store the data in the list data structure is much more efficient. Thanks @DMGregory \$\endgroup\$ – SidS Jan 18 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.