PlayerPrefs base saving system

Hello, I am preparing a mobile game that includes a shop. I save my shop state with a string, to save this string I use PlayerPrefs. My game works and saves everything correctly in the editor but when I build apk problem is starting the problem is; game saves shop state correctly when the game is running but when I quit the game my shop state turns to default (so every item that player bought are deleted). But interesting thing is coins and high score do not go. I use PlayerPrefs.Save() when needed I even try it in Update() but nothing changes.

    public string content = "hope";

    public void Load(ShopItem item) // works when shop run.
    {
        int itemIndexInt = item.itemIndex;
        int itemIsBoughtInt = item.isBought;
        string contentPart = itemIndexInt.ToString() + itemIsBoughtInt.ToString();
        content += contentPart + ITEM_SEPERATOR;
        PlayerPrefs.SetString("content", content);
        PlayerPrefs.Save();
    }

    public void Save(ShopItem item) //works when user buy or equip something (onclick)
    {
      
        string[] itemsArray = PlayerPrefs.GetString("content").Split(new[] { ITEM_SEPERATOR }, System.StringSplitOptions.None);

        for(int i = 0; i < itemsArray.Length ; i++)
        {
            if (Equals(itemsArray[i], item.itemIndex.ToString() + item.isBought.ToString()))
            {
                itemsArray[i] = item.itemIndex.ToString() + item.isBought.ToString();
                content = content.Replace(itemsArray[i] ,item.itemIndex.ToString() + "1");
                item.isBought = 1;
                PlayerPrefs.SetString("content", content);
                PlayerPrefs.Save();
            }
        }
    }

The string that I want to save is content thanks all for helping.

Hi Onur,

Welcome to our community, and thank you for your message.

With 250,000+ students to support I’m afraid we can’t help out with issues that are outside the scope of the course.

Please feel free to ask for advice from our helpful community of students over on our Discord chat server.

Good luck :slight_smile:

This topic was automatically closed after 34 days. New replies are no longer allowed.

Privacy & Terms