[Solved] Text does not update when pressing S key No errors

When running the game in Unity and pressing the S key to go to the sheets_0 state nothing happens. It displays the text for the cell state but does not update to the sheets_0 state. I added a line to output to the debug console if Unity detects the S key is pressed, which it does (see screenshot). but does not progress beyond that.
I am on Unity version 5.4 1f1.


using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextController : MonoBehaviour {

public Text text;
private enum States {Cell, mirror, cell_mirror, lock_0, lock_1, sheets_0, sheets_1, freedom}
private States MyStates;

// Use this for initialization
void Start () {

MyStates = States.Cell;
Debug.Log(MyStates);
}

// Update is called once per frame
void Update ()
{

	if (MyStates == States.Cell) {
		State_Cell ();
	} else if (MyStates == States.sheets_0) {
	state_sheets_0();
	}

}
void State_Cell ()
{
	text.text = "You are in your prison cell and you want to escape There are" +
	" some dirty sheets and a mirror on the wall and the door " +
	"is locked \n\n " +
	"Press S to view Sheets, M to view Mirror, and L to view Lock";

	if (Input.GetKeyDown (KeyCode.S)) {
	Debug.Log("S Key Pressed");
	MyStates = States.sheets_0;
	}
}
void state_sheets_0 (){
	text.text = "You are in your prison cell and you want to escape There are" +
                " some dirty sheets and a mirror on the wall and the door " +
				"is locked \n\n " + 
				"Press S to view Sheets, M to view Mirror, and L to view Lock";

	if(Input.GetKeyDown(KeyCode.C)){
		MyStates = States.Cell;
	}
}

}

Hi @Kenny_Nichols,

Looking at your code, you currently only have methods for two of the states, e.g. in the cell, and sheets_0 - but you have the exactly same text for both, so chances are it is responding to your key press (as indicated by the console message) but then because you are outputting the exact same text to the screen there is no obvious change.

Just as a quick test, in your void state__sheets_0() method, add your name in front of the “You are in…” e.g.
text.text = "Kenny, you are in.."

then when you run the game, look for the change in the text…

Wow!! 1. Thanks for the VERY quick response and 2. For whatever reason my brain saw the sheets_0 text. I’ve actually gone through this section before and was looking at my completed version as a reference that that is what threw me off.
But on the bright side it was a good exorcise of using the debug console verify that input was being detected. :slight_smile:

Thank you so much!!

1 Like

You’re more than welcome Kenny :slight_smile:

Privacy & Terms