What I am Trying to do is that I have a choose the gift UI, where when I click on a gift(it is a button which runs a function for selecting that gift and then tries to save it in a JSON file, "Serialization"). It is throwing an error,
IndexOutOfRangeException: Index was outside the bounds of the array. The code for serializing is:
public void UnlockRewardedGift()
{
StartCoroutine(AudioManager.instance.Play("SmallButton"));
Ads_Manager.Instance.isGiftUnlocked = true;
Ads_Manager.Instance.ShowUnityRewardedVideoAd();
UserData data = new UserData();
string json = JsonConvert.SerializeObject(ShopManager.Instance._GiftNumber, Formatting.Indented);
var path = Path.Combine(Application.persistentDataPath, "saveData.json");
File.WriteAllText(path, json);
PlayerPrefs.SetInt("Gift" + ShopManager.Instance.unlockedGift + "Bought", 1);
ShopManager.Instance.isRewarded = false;
ShopManager.Instance.GiftStatusFetcher();
CloseRewardPanel();
}
This is the UserData class where error is thrown on the line (_GiftNumber[i] = true;):
using System;
using System.Collections.Generic;
using MyCode;
using Newtonsoft.Json;
[Serializable]
public class UserData
{
//public int _LastSelected { get; set; }
public bool[] _GiftNumber { get; set; }
public UserData()
{
_GiftNumber = new bool[16];
for(int i =0; i<=_GiftNumber.Length; i++)
{
_GiftNumber[i] = true;
}
}
}
Once the specific index has been set to true it must be loaded on the shopManager bool array Object named _GiftNumber[] :
string fileName = "saveData.json";
var path = Path.Combine(Application.persistentDataPath, fileName);
if(File.Exists(path))
{
string jsonText = File.ReadAllText(fileName);
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<UserData>(jsonText);
}
When I open the Home Scene which contains the ShopManagerScript (the one in which I'm trying to set the bool to true), I am deserializing the Json in the start method of this script and this is the error I get thrown when I open this scene:
` FileNotFoundException: Could not find file "D:\Spartans Global\Projects\Current Project\Pinpull-V5\saveData.json"
Thanks in advance! I've been stuck with this for a while and would really appreciate some help. I'm a newbie tried few things but cant seem to figure out that's why reaching out to this amazing community. Regards.