Out of bounds error when trying to switch States

When im trying to go to the next state i keep getting the error message that the array is out of bounds, even though the array is 0 - 1
here is my code

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

public class AdventureGame : MonoBehaviour
{

    [SerializeField] Text textComponent;
    [SerializeField] States startingState;

    States state;
    // Start is called before the first frame update
    void Start()
    {
        state = startingState;
        textComponent.text = state.getStateStory();
    }

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

    private void MangageState()
    {
        States[] nextUp = state.getNextStates();
        if(Input.GetKeyDown(KeyCode.Alpha1))
        {
            state = nextUp[0];
        }
    }
    
}

any ideas for what im supposed to do and yes im pressing down ON the button but even if i do it brings up this error…

1 Like

Are you calling the correct index number? Is your syntax correct for calling getNextStates? Is Alpha1 an actual KeyCode you can use given your current setup? Also the console log is your friend when finding out some bugs. I assume you have found the issue already, just remember to use every check you can think of to troubleshoot. Good luck.

Edit: Check of your array elements have any State objects stored too. That could be an issue for you.

Hey! I ran into this issue too. My solution was that I forgot to drag and drop Room 1 and Room 2, and then Starting room from the Assests Browser to the “Next Stage” serialized addition we made in the Inspector. So its wasn’t code related as much the Inspector window needing the next stages added into it in each room scriptable object.

Hope that helps!

2 Likes

Privacy & Terms