Game states don't cycle through

Going through the Text101 course and I’m having a bit of an issue with states switching when keys are pressed. I have all my states connected, and my code matches what’s been done in this lesson. On my intro scene, I press 1 to change scenes, the scene changes for a split second then quits and playmode ends. The only difference in my AdventureGame.CS code is I have debug lines to see if it’ll give any errors. it recognizes the correct key is pressed, but that’s it. Any help would be appreciated. :slight_smile:

AdventureGame.cs ManageState called in void Update()

    private void ManageState()
    {
        var nextStates = state.GetNextStates();
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            state = nextStates[0];
            Debug.Log("1 pressed");
        }
        else if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            state = nextStates[1];
            Debug.Log("2 pressed");
        } 
        textComponent.text = state.GetNextState();
    }
}

and here is my State.cs

[CreateAssetMenu(menuName = "State")]
public class State : ScriptableObject
{
    [TextArea(10,14)] [SerializeField] string storyText;
    [SerializeField] State[] nextStates;

    public string GetNextState()
    {
        return storyText;
    }

    public State[] GetNextStates()
    {
        return nextStates;
    }
}

Hi cpagan,

I don’t see any problem in your code. Are there any error messages in your console?

Hi Nina-

The only thing that shows up in my console is the appropriate message when I press either 1 or 2.

Are all message types enabled in your console?

Since your code looks fine, I’m wondering why Unity stops the game. It usually does that only when there is an issue. Without any error messages and with fine code, it is almost impossible to figure out what might be causing the problem.

And if we cannot figure out the problem and if everything appears to be fine, I would suggest to remove the State objects from the Next States fields in each State object and to assign them. Sometimes, Unity is a bit buggy.

Yes ma’am i am certain all message types are enabled.
I’ll try your suggestion of removing each state from the next state field and reasigning them. If that doesn’t work im tempted to rebuild from scratch or just move on as i understand the concepts here

So I was able to resolve this. I must have changed the hotkeys for entering play mode at one point, and it turned out 1 was play and 2 was pause. Once I remapped those, everything worked!

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

Privacy & Terms