Samuel J Samuel J - 1 month ago 10
C# Question

Having issues with State changes in a Text Adventure in Unity 4.6

Basically, I'm trying to get Unity to change the game's state when the player clicks a button. For whatever reason though, it isn't reading the second key press that actually changes the state. The code looks like this:

void state_Cell () {
if (CelP == 0) {
text.text = "Some text";
if (Input.GetKeyDown (KeyCode.C)) { //This works
++CelP;
text.text = "Some more text.";
if (Input.GetKeyDown (KeyCode.B)) { //This doesn't work
++CelP;
MyState = States.Bed;
}
}
}
}


If it will help, this is the code for the Bed state that I can't access in the game:

void state_Bed () {
if (BedP == 0) {
text.text = "Still more text.";
if (Input.GetKeyDown (KeyCode.D)) {
MyState = States.Delivery;
BedP++
}
}
}


The most confusing part about this for me is that Unity's compiler isn't giving me any errors. I encountered this problem with a different state before, and what I did was move the "++Variable" to a different location, but that didn't work for this statement.

Aside from that, I've also tried to removing the "if statement" in the Bed state, but that didn't work either.

Another thing that I attempted was changing "B" in the "KeyCode" to different keys, but that didn't work at all.

I also tried to include a "print();" statement at the very beginning of the Bed state, but it didn't trigger either.

The last thing that I've tried was putting a "print();" statement after the "(KeyCode.B))", but that didn't work either. So from that I know that the problem is that Unity isn't reading or responding to the key press.

Any help would be appreciated. And you need anymore information than what I've supplied, tell me and I'll give it to you.

Thanks in advance.

Answer

You should create two states for Cell, first one listens to C and changes the state to second, second one listens to B and changes the state to Bed.

The reason is that state machines do not follow after where they left in the middle of the code inside one state. You need to be more specific when you are defining your states.

I assume that state_Cell is being called from an Update or a similar Coroutine which is being called every x frame. so local variables inside state_Cell is reset every time it is called. so machine does not know if user previously pressed C or did not press anything.

When user presses a key then technically the state has been changed. when user presses another key then again state changes.

Edit

In case you don't want to add extra states this is the simplified version of your state_Cell

You have to be careful when to set the extra Boolean to false.

bool c_pressed = false;
void state_Cell () {
        if (c_pressed || Input.GetKeyDown (KeyCode.C)) {            
           c_pressed = true;
            if (Input.GetKeyDown (KeyCode.B)) {
               c_pressed = false;
                MyState = States.Bed;
            }
        }
        else
             c_pressed = false;
    }
}