Index was outside the bounds of the array. C# Unity player prefs
Пишу текстовый квест на юнити, делаю сейчас сохранение и загрузку. Вот код основного скрипта:
using System;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
[Serializable]
public class Action
{
public string Description;
public byte Index;
public AudioClip ClickClip;
}
[Serializable]
public class Room
{
public string Description;
public Action[] Actions;
public Texture BG;
public AudioClip Clip;
}
public class TextQuest : MonoBehaviour
{
[SerializeField]
private TMP_Text _roomDesc;
[SerializeField]
private Button[] _actionButtons;
[SerializeField]
private TMP_Text[] _actionTexts;
[SerializeField]
private RawImage _background;
[SerializeField]
private AudioSource _bgAudio;
[SerializeField]
private AudioSource _clickAudio;
[SerializeField]
private Room[] _roomInfo;
[SerializeField]
private int _currentIndex = 0;
[SerializeField]
private byte savedIndex;
private void SetRoomInfo()
{
var currentRoom = _roomInfo[_currentIndex];
var currentRoomActions = currentRoom.Actions;
_roomDesc.text = currentRoom.Description;
_background.texture = currentRoom.BG;
for (var i = 0; i < _actionButtons.Length; i++)
{
_actionButtons[i].gameObject.SetActive(false);
}
for (var i = 0; i < currentRoomActions.Length; i++)
{
_actionTexts[i].text = currentRoomActions[i].Description;
_actionButtons[i].gameObject.SetActive(true);
}
_bgAudio.clip = currentRoom.Clip;
_bgAudio.Play();
}
private void EndGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex -1);
}
private void OnActionButton(byte index)
{
var currentAction = _roomInfo[_currentIndex].Actions[index];
_currentIndex = currentAction.Index;
_clickAudio.PlayOneShot(currentAction.ClickClip);
if (_currentIndex >= _roomInfo.Length)
EndGame();
else
SetRoomInfo();
}
private void Start()
{
SetRoomInfo();
for (byte i = 0; i < _actionButtons.Length; i++)
{
var index = i;
_actionButtons[i].onClick.AddListener(() => OnActionButton(index));
}
}
public void Save()
{
PlayerPrefs.SetInt("savedIndex", _currentIndex);
}
public void Load()
{
_currentIndex = PlayerPrefs.GetInt("savedIndex");
}
}
Когда открываю меню, в котором кнопки сохранения и загрузки выдает ошибку
(жалуется на строку var currentRoom = _roomInfo[_currentIndex];
):
IndexOutOfRangeException: Index was outside the bounds of the array.
TextQuest.SetRoomInfo () (at Assets/Scripts/TextQuest.cs:57)
TextQuest.Start () (at Assets/Scripts/TextQuest.cs:98)
Хотя до того момента как в меню не захожу ее нету. Помогите пожалуйста.