Click here to Skip to main content
15,881,630 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I started using PlayerPrefs in Unity and everything worked perfectly. Then I tried to build the game to send it to a friend. *But* as I tested it one more time (build version). The PlayerPrefs stopped working. It's just, like they never existed. Saving between Scenes does not work. Back in the Unity Editor, everything works. But why?

Hope somebody can help me, so I can play my game outside the editor 💀.

Kind regards.

--------

Ok, restarted unity, and now I am getting an error, the game in and outside Unity malfunctions now the same. But why (and why tf did it work like a charm before I restarted and Unity recognized the error)? Please help.

FormatException: Input string was not in a correct format.
System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Int32.Parse (System.String s) (at <695d1cc93cca45069c528c15c9fdd749>:0)
UI_Inventory.RefreshInventoryItemsNewWorld () (at Assets/SinglePlayer/Scripts/Inventory/UI_Inventory.cs:123)
UI_Inventory.Start () (at Assets/SinglePlayer/Scripts/Inventory/UI_Inventory.cs:24)


--------

Here is the script I save in:

C#
using System.Collections;
        using System.Collections.Generic;
        using UnityEngine;
        using UnityEngine.UI;
        using CodeMonkey.Utils;
        using TMPro;
        
        public class UI_Inventory : MonoBehaviour
        {
            private Inventory inventory;
            private Transform itemSlotContainer;
            private Transform itemSlotTemplate;
            private PlayerController player;
        
            private void Awake() {
                itemSlotContainer = transform.Find("itemSlotContainer");
                itemSlotTemplate = itemSlotContainer.Find("itemSlotTemplate");
            }
        
            private void Start()
            {
                RefreshInventoryItemsNewWorld();
            }
        
            public void SetPlayer(PlayerController player)
            {
                this.player = player;
            }
        
            public void SetInventory(Inventory inventory) 
            {
                this.inventory = inventory;
        
                inventory.OnItemListChanged += Inventory_OnItemListChanged;
        
                RefreshInventoryItems();
            }
        
            private void Inventory_OnItemListChanged(object sender, System.EventArgs e)
            {
                RefreshInventoryItems();
            }
        
            private void RefreshInventoryItems()
            {
                foreach (Transform child in itemSlotContainer)
                {
                    if (child == itemSlotTemplate) continue;
                    Destroy(child.gameObject);
                }
        
                int x = 0;
                int y = 0;
                float itemSlotCellSize = 90f;
        
                foreach (Item item in inventory.GetItemList())
                {
        
                    RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent<RectTransform>();
                    itemSlotRectTransform.gameObject.SetActive(true);
        
                    itemSlotRectTransform.GetComponent<Button_UI>().ClickFunc = () => 
                    {
                        inventory.UseItem(item);
                    };
        
                    itemSlotRectTransform.GetComponent<Button_UI>().MouseRightClickFunc = () => 
                    {
                        Item duplicateItem = new Item { itemType = item.itemType, amount = item.amount };
                        inventory.RemoveItem(item);
                        ItemWorld.DropItem(player.GetPosition(), duplicateItem);
                    };
        
                    itemSlotRectTransform.anchoredPosition = new Vector2(x * itemSlotCellSize, y * itemSlotCellSize);
                    Image image = itemSlotRectTransform.Find("image").GetComponent<Image>();
                    image.sprite = item.GetSprite();
        
                    TextMeshProUGUI uiText = itemSlotRectTransform.Find("text").GetComponent<TextMeshProUGUI>();
                    if (item.amount > 1)
                    {
                        uiText.SetText(item.amount.ToString());
        
                        // sets player pref
                        PlayerPrefs.SetString(item.itemType.ToString(), item.amount.ToString());
        
                        PlayerPrefs.Save();
                    } else {
                        uiText.SetText("");
                    }
        
                    x++;
                    if (x > 4) {
                        x = 0;
                        y++;
                    }
        
                }
                
            }
        
            private void RefreshInventoryItemsNewWorld()
            {
                foreach (Transform child in itemSlotContainer)
                {
                    if (child == itemSlotTemplate) continue;
                    Destroy(child.gameObject);
                }
        
                int x = 0;
                int y = 0;
                float itemSlotCellSize = 90f;
        
                foreach (Item item in inventory.GetItemList())
                {
                    item.amount = int.Parse(PlayerPrefs.GetString(item.itemType.ToString()));
        
                    RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent<RectTransform>();
                    itemSlotRectTransform.gameObject.SetActive(true);
        
                    itemSlotRectTransform.GetComponent<Button_UI>().ClickFunc = () => 
                    {
                        inventory.UseItem(item);
                    };
        
                    itemSlotRectTransform.GetComponent<Button_UI>().MouseRightClickFunc = () => 
                    {
                        Item duplicateItem = new Item { itemType = item.itemType, amount = item.amount };
                        inventory.RemoveItem(item);
                        ItemWorld.DropItem(player.GetPosition(), duplicateItem);
                    };
        
                    itemSlotRectTransform.anchoredPosition = new Vector2(x * itemSlotCellSize, y * itemSlotCellSize);
                    Image image = itemSlotRectTransform.Find("image").GetComponent<Image>();
                    image.sprite = item.GetSprite();
        
                    TextMeshProUGUI uiText = itemSlotRectTransform.Find("text").GetComponent<TextMeshProUGUI>();
                    if (item.amount > 1)
                    {
                        uiText.SetText(item.amount.ToString());
        
                        // sets player pref
                        PlayerPrefs.SetString(item.itemType.ToString(), item.amount.ToString());
        
                        PlayerPrefs.Save();
        
                    } else {
                        uiText.SetText("");
                    }
        
                    x++;
                    if (x > 4) {
                        x = 0;
                        y++;
                    } 
                }
            }
        }


What I have tried:

I have tried to restart unity and went through all different types of PlayerPref methods. I am kind of new to Unity, so please excuse beginner errors 😅.
Posted
Updated 30-Oct-22 12:14pm
v2

1 solution

Restarting won't help: the error is saying that the string you have provided does not contain a valid number.
So look at the code, and find line 123 of file UI_Inventory.cs: it's probably this one:
item.amount = int.Parse(PlayerPrefs.GetString(item.itemType.ToString()));
So use the debugger, and find out exactly what is being passed to int.Parse, then you can start working out why it's not a number, and what it should be.

Sorry, but we can't do any of that for you!
 
Share this answer
 
Comments
jcjms 31-Oct-22 8:11am    
Yeah, you are right. Restarting made it worse, lol. Anyway, thank you for trying to help. I will test.
OriginalGriff 31-Oct-22 8:25am    
Start with the debugger and find out exactly what you are trying to convert - that should give you a clue as to why it's not what you expected.
jcjms 31-Oct-22 8:39am    
Damn, sorry for wasting your time, I was just way to tired. I just saw, that I wanted to convert an Int to a String (PlayerPrefs.SetString(item.itemType.ToString(), item.amount.ToString());), just to convert it back from a string to an int later (item.amount = int.Parse(PlayerPrefs.GetString(item.itemType.ToString()));).

But thank you for your help, kind regards.
OriginalGriff 31-Oct-22 8:56am    
You're welcome!
OriginalGriff 31-Oct-22 8:56am    
Not once an answer has been posted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900