Hi,
I’m having an issue with the AdventureGame.cs to get the next state from keyboard input. My code is like this:
void Update()
{
ManageState();
}
private void ManageState()
{
var nextStates = state.GetNextStates();
if (Input.GetKeyDown(KeyCode.Alpha1))
{
state = nextStates[0];
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
state = nextStates[1];
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
state = nextStates[2];
}
textComponent.text = state.GetStateStory();
}
And GetNextStates
is defined in State.cs as:
public State[] GetNextStates()
{
return nextStates;
}
but when I run this on Unity and press the alphanumeric 1 key, I’m always getting the same error saying that the index is out of bounds.
IndexOutOfRangeException: Index was outside the bounds of the array.
AdventureGame.ManageState () (at Assets/AdventureGame.cs:54)
AdventureGame.Update () (at Assets/AdventureGame.cs:45)
Indeed, when I do Debug.Log(nextStates.Length);
the value is always 0, so it seems the array is not being created properly.
How to solve this issue?
Many thanks,
Martim