0

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.

  • 2
    The `for` loop in the constructor is wrong, it should be `for(int i =0; i <_GiftNumber.Length; i++)` (I.e. **<** not **<=**). Thus this looks to be a duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/q/20940979/3744182). – dbc Nov 18 '21 at 06:33
  • Okay I'll check that question. Thankyou for sharing. – Burhan Ahmad Nov 18 '21 at 06:48
  • @dbc The error I mentioned is not occuring now, i changed the _GiftNumber[i] to _Giftnumber[0] = true; and _GiftNumber[1] = true now I only get error when the deserialization is done. It says the the json file could not be found so that means my json file is not being created in the first place. Any idea about that? – Burhan Ahmad Nov 18 '21 at 07:04
  • @derpirscher, no sir – Burhan Ahmad Nov 18 '21 at 07:04
  • 2
    The second error is a **typo** / logical mistake .. you do `File.ReadAllText(fileName)` but you rather wanted `File.ReadAllText(path)` – derHugo Nov 18 '21 at 07:17

0 Answers0