A
A
Artyom2022-01-16 17:38:38
C++ / C#
Artyom, 2022-01-16 17:38:38

How to create a save using PlayerPrefs?

When creating, the developer who explains the principle of saving refers to the List:

public List<Item> shopItems = new List<Item>();
private Save sv = new Save();
 sv = JsonUtility.FromJson<Save>(PlayerPrefs.GetString("SV"));
            score = sv.score;
            for (int i = 0; i < shopItems.Count; i++)
            {
                shopItems[i].levelOfItem = sv.levelOfItem[i];
                shopItems[i].bonusCounter = sv.bonusCounter[i];
                if (shopItems[i].needCostMultiplier) shopItems[i].cost *= (int)Mathf.Pow(shopItems[i].costMultiplier, shopItems[i].levelOfItem);
                if (shopItems[i].levelOfItem != 0) scoreIncrease += (int)Mathf.Pow(shopItems[i].bonusIncrease, shopItems[i].levelOfItem);
public class Save
{
    public int score;
    public int[] levelOfItem;
    public int[] bonusCounter;
}


In my case, I need to save the number of points and "Workforce", which gives a passive accumulation of points. My code :
public int score;
 public int bonus = 1;
 public int WorkersCount;

  public Save SaveGame = new Save();
 private void OnApplicationQuit()
   {    
      SaveGame.score = score;
      SaveGame.WorkersCount = new int [WorkersCount.Count];
      for (int i = 0; i < WorkersCount.Count; i++)
      {
         SaveGame.WorkersCount[i] = WorkersCount[i].WorkersCount;
      }
      PlayerPrefs.SetString("GameSave", JsonUtility.ToJson(SaveGame));
   }

   [Serializable]
   public class Save
   {
      public int score;
      public int [] WorkersCount;
   }


In my case, the engine swears at me:
1. Cannot apply indexing with [] to an expression of type 'int
2.'int' does not contain a definition for 'Count' and no accessible extension method 'Count' accepting a first argument of type 'int' could be found

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question