Easy Save 3 Saving Bool Array

Hey im trying to save a bool array for if a top or shorts has been unlocked. I have the bool in a Scriptable Object and am using a foreach loop to save each thing in it for each SO

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterStatsSaveLoad : MonoBehaviour
{
    public CharacterSO[] Characters;
    public bool[] tets;

    public void SaveAll()
    {
        foreach(CharacterSO Chrt in Characters)
        {
            ES3.Save("LoyaltyLevel" + Chrt.Name, Chrt.LoyaltyLevel);
            ES3.Save("Loyalty" + Chrt.Name, Chrt.Loyalty);
            ES3.Save("Active" + Chrt.Name, Chrt.Active);
       
            ES3.Save("TopNum" + Chrt.Name, Chrt.TopNum);
            ES3.Save("ShortsNum" + Chrt.Name, Chrt.ShortsNum);
    
            foreach(bool TopUlk in Chrt.TopUnlocked)
            {
                ES3.Save("TopUnlocked" + Chrt.Name + TopUlk, TopUlk);
            }

            foreach (bool ShortsUlk in Chrt.ShortsUnlocked)
            {
                ES3.Save("ShortsUnlocked" + Chrt.Name + ShortsUlk, ShortsUlk);
            }

        }
    }
    public void LoadAll()
    {
        foreach (CharacterSO Chrt in Characters)
        {
            if (ES3.KeyExists("LoyaltyLevel" + Chrt.Name))
            {
                Chrt.LoyaltyLevel = ES3.Load<int>("LoyaltyLevel" + Chrt.Name);
            }
            if (ES3.KeyExists("Loyalty" + Chrt.Name))
            {
                Chrt.Loyalty = ES3.Load<int>("Loyalty" + Chrt.Name);
            }
            if (ES3.KeyExists("Active" + Chrt.Name))
            {
                Chrt.Active = ES3.Load<bool>("Active" + Chrt.Name);
            }
      
            if (ES3.KeyExists("TopNum" + Chrt.Name))
            {
                Chrt.TopNum = ES3.Load<int>("TopNum" + Chrt.Name);
            }
            if (ES3.KeyExists("ShortsNum" + Chrt.Name))
            {
                Chrt.ShortsNum = ES3.Load<int>("ShortsNum" + Chrt.Name);
            }
            foreach (bool TopUlk in Chrt.TopUnlocked)
            {
                if (ES3.KeyExists("TopUnlocked" + Chrt.Name + TopUlk))
                {
                    TopUlk = ES3.Load<bool>("TopUnlocked" + Chrt.Name + TopUlk);
                }

            }
            foreach (bool ShortsUlk in Chrt.ShortsUnlocked)
            {
                if (ES3.KeyExists("ShortsUnlocked" + Chrt.Name + ShortsUlk))
                {
                    ShortsUlk = ES3.Load<bool>("ShortsUnlocked" + Chrt.Name + ShortsUlk);
                }

            }



        }
     
    }
}

This is the code im using however im getting this error

Assets/Character Stats Package/Scripts/CharacterStatsSaveLoad.cs(70,21): error CS1656: Cannot assign to ‘ShortsUlk’ because it is a ‘foreach iteration variable’

I think there is a way Easy Save can save arrays but i want it to when i add more tops and shorts it will still be able to load them and wont get an error as its trying to load onto array elements that werent previously there

Easily Google-able problem, posting to the forums should not be your first thought. You cannot change the variable of something you’re iterating through in a foreach loop. You can just change it to a regular for loop and it would work.

1 Like