Error: Index was outside the bounds of the array

(Unable to switch between states)
Here, is the code…

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

public class printHere : MonoBehaviour
{

[SerializeField] Text StoryLine;
[SerializeField] State StartingState;

State state;


// Start is called before the first frame update
void Start()
{
    state = StartingState;
    StoryLine.text = state.GetStateStory();
   
}

// Update is called once per frame
void Update()
{
    ManageStates();
}

private void ManageStates()
{
    var nextStates = state.GetNextStates();

    if(Input.GetKeyDown(KeyCode.Q))
    {
        state = nextStates[0];
    }
    else
    if(Input.GetKeyDown(KeyCode.W))
    {
        state = nextStates[1];
    }
    else
    if(Input.GetKeyDown(KeyCode.E))
    {
        state = nextStates[2];
    }
}

}

Hi Vikash,

That’s a known issue, which gets solved in lecture “For Loops”. Till then, make sure that all your State objects in your Assets folder have got “next states” in their Next States array in their respective Inspector.

Did this help?

Yes!! Problem solved…
Thank you!!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms