Hello, Im stuck with this error:
I have a problem, in my game, when I click a button, I get an “Index was out of range error”, I know these errors are caused by trying to get the N+1 element of an array with only N elements, however I triple checked my code and I cant find a single place where I could be getting this error.
This is the relevant code:
public void SpawnRandomLevelCards()
{
FirebaseDatabase.DefaultInstance.GetReference("Levels").GetValueAsync().ContinueWith(task =>
{
if(task.IsFaulted || task.IsCanceled)
{
Debug.Log("ERROR: Couldnt load levels");
}
else if (task.IsCompleted)
{
Debug.Log("Loaded levels properly!");
long totalLevelsCreated = task.Result.ChildrenCount;
long levelsToLoad = 10;
if (totalLevelsCreated < 10) levelsToLoad = totalLevelsCreated;
Debug.Log("total levels created: " + totalLevelsCreated + "Levels to load: " + levelsToLoad);
tilemapDataOnlineLevels = new List<TilemapData>();
for (int i = 0; i < levelsToLoad; i++)
{
string json = task.Result.Child(i.ToString()).GetRawJsonValue();
TilemapData data = JsonUtility.FromJson<TilemapData>(json);
tilemapDataOnlineLevels.Add(data);
}
finishedLoading = true;
}
});
}
private void Update()
{
if (finishedLoading)
{
for (int i = 0; i < tilemapDataOnlineLevels.Count; i++)
{
GameObject card = Instantiate(levelCardPrefab, content);
card.transform.GetChild(0).GetComponent<TMP_Text>().text = tilemapDataOnlineLevels[i].levelName;
card.transform.GetChild(2).GetComponent<TMP_Text>().text = "Online ID: " + tilemapDataOnlineLevels[i].onlineLevelID;
Button playBttn = card.transform.GetChild(1).GetComponent<Button>();
playBttn.onClick.AddListener(() => { PlayOnlineLevel(tilemapDataOnlineLevels[i].onlineLevelID, tilemapDataOnlineLevels[i].levelName); });
playBttn.onClick.AddListener(() => { PlayButtonSFX(); });
Debug.Log("Created card");
}
//PlayOnlineLevel(tilemapDataOnlineLevels[0].onlineLevelID, tilemapDataOnlineLevels[0].levelName);
finishedLoading = false;
}
}
public void PlayOnlineLevel(int id, string name)
{
DataTransfer.Instance.SetLevelID(id, name, true);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 2);
}
public void PlayButtonSFX()
{
SFXManager.Instance.PlayUIButtonClickSFX(SFXManager.Instance.gameObject.transform);
}
The error in the console doesnt show in which line the error is produced but it occurs when I click the button called “playBttn”
Note: I am using void Update() because Unity wouldnt let me Instantiate objects inside Asyncs or inside functions called from Asyncs (I dont know why this happens but changing it to the Update method worked and the cards are Instantiated correctly)
I attached an image of what I am getting in the console.
Just to clarify if it wasnt already clear: this error only pops up when I click the button (refered to as “playBttn” in the code) in playtime.
Also, you can see in the code that I have commented out a line, in which I call the same Method as when I click the button, I tested to see if it would actually call the method or not, and indeed it does. So I have no clue why the same Method wont work when called from a button click.
Any help will be appreciated