Key press not working

Good afternoon, I can’t seem to be able to get the from the starting state to any of the other states. Room 1 and such.

My code appears correct. The elements and objects appear to be assigned correctly. I have tried every solution I found on the forum and the udemy course page.

Here is my code. I hope someone can help me out. Really want to continue the course.


using UnityEngine;

[CreateAssetMenu(menuName = “State”)]
public class State : ScriptableObject
{

[TextArea(14, 10)] [SerializeField] string storyText;
[SerializeField] State[] nextStates;

public string GetStateStory()
{
    return storyText;
}

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

}


using UnityEngine;
using UnityEngine.UI;

public class AdventureGame : MonoBehaviour
{

[SerializeField] Text textComponent;
[SerializeField] State startingState;



State 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()
{
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];

    }

    textComponent.text = state.GetStateStory();

}

}

Hi Tony,

Welcome to our community! :slight_smile:

When you try to access another state, do you get an error message? Maybe a NullReferenceException?

If you don’t get an error message at all, log a message into your console when you press the keys. If no message appears, check the spelling of your methods. Remember you can also look at the lecture code changes via the link in the Resources of each lecture.


See also:

1 Like

Thank you! That fixed it!

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

Privacy & Terms