[VISUAL ERROR] Text not updating correctly

At the outset of this lesson I thought I’d use what I learned in the previous lesson to simply adapt the simple code from the first exercise into something that runs as a program. I followed similar procedures to the text adventure game, however, there is a problem that arises with my code that doesn’t happen when I run the text adventure program, it’s hard to describe but the window doesn’t seem to be updating the text field correctly.

As you can see it has added the next text to the field, but the old text remains behind it for some reason. Is this a common bug, or have I messed something up spectacularly?

Have a look in the editor window while you play the game to get a look at what text boxes are there in the scene.

it does look like there is multiple UI Text boxes on top of each other. (starting game, and gameplay boxes from what I can read there)

it is possible that you may be updating the wrong text box, if some are meant to be empty until a certain point.

There’s only one text box. It’s using the same kind of state logic as the text adventure game to fill them in. I tried adding a text.text = “”; in each of the states to clear it before filling it but it doesn’t appear to have any effect. I should say that everything runs perfectly fine in the Unity editor, it’s only when I export it as an exe that this problem occurs.

just one scene in your build settings?

Yep. I honestly wouldn’t even know how to add additional scenes. As far as I can tell it works exactly like the text adventure game did. Here’s the code:

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

public class TextController : MonoBehaviour {

	public Text text;
	public enum States {menu, game, win};
	public States myState;
	public int min = 1;
	public int max = 1001;
	public int guess = 500;


	// Use this for initialization
	void Start () {
		myState = States.menu;

	}
	
	// Update is called once per frame
	void Update () {
		print (guess);
		if (myState == States.menu) {
			text.text = "";
			state_menu ();	
		} else if (myState == States.game) {
			text.text = "";
			state_game ();
		} else if (myState == States.win) {
			text.text = "";
			state_win ();
		}

		if (Input.GetKeyDown (KeyCode.Escape)) {
			Application.Quit();
		}
		
	}

	void state_menu () {
		text.text = "Welcome to Number Wizard!\n\n Please press [Return] " +
					"to continue or [Esc] to exit.";
		if (Input.GetKeyDown (KeyCode.Return)) {
			myState = States.game;
		}  
	}

	void state_game () {
		guess = (max + min) / 2;
		if (guess == 999 && max == 1000) {
			max = max + 1;
		}
		text.text = "Please select a number between  1 and 1000.\n\n" +
					"Is your number " + guess + "?\n\n" +
					"Please press [H] for highter, [L] for lower, or [return] for correct.";
		if (Input.GetKeyDown (KeyCode.H)) {
			min = (max + min)/2;
		} else if (Input.GetKeyDown (KeyCode.L)) {
			max = (guess);
		} else if (Input.GetKeyDown (KeyCode.Return)) {
			myState = States.win;
		}
	}

	void state_win () {
		max = 1000;
		min = 1;
		text.text = "Haha! I've won!\n\n" +
					"Would you like to play again?\n\n" +
					"Press [R] to play again. Press [Esc] to quit";
		if (Input.GetKeyDown (KeyCode.R)) {
			myState = States.game;
		}

	}
}

And the code has no trouble functioning in the Unity preview. So I can’t imagine it’s even any problem with the code. It’s just once it’s built that it gets weird.

In any case it’s just using the standard text.text = “text” thing from the last lesson.

Ah! This must be a problem outside of Unity. If I make the window resizable, and resize the window then it clears the old text. That should mean it’s a problem with my graphic drivers?

not sure, sounds strange tho. ill have a read around.

least youve pinpointed a problem tho

Man, any way I look at this it’s totally weird. The text adventure uses the following code to change states:

	void state_lock_0 () {
		text.text = "The door is locked. You don't have anything to pick it with. Perhaps " +
					"if you had something sharp you could do something about it!\n\n" +
					"Press 'r' to return.";
		if (Input.GetKeyDown ( KeyCode.R)) {
			myState = States.cell;
		}
	}

And my program uses the following code to call states:

void state_menu () {
		text.text = "Welcome to Number Wizard!\n\n Please press [Return] " +
					"to continue or [Esc] to exit.";
		if (Input.GetKeyDown (KeyCode.Return)) {
			myState = States.game;
		}  
	}

But the text adventure works fine once built, but my code leaves behind the previous state’s text.

how do those the code look? do you have several boxes text boxes in the same place? Try removing the old text in the code with // in front if you don’t want to delete it.

try changing the void Update tho this…
print (guess);
if (myState == States.menu) {
state_menu ();
} else if (myState == States.game) {
state_game ();
} else if (myState == States.win) {
state_win ();
}

if (Input.GetKeyDown (KeyCode.Escape)) {
	Application.Quit();
}

try this on Update in case that the last one didn’t work.

Uppdate

That’s the same thing that I had in my original code though?

I did end up finding the answer to this. Apparently I deleted the main camera from the scene, which causes the exact behavior I described. Something for other newbies to be on the lookout for, still strange that the behavior only shows up once the game is built into a standalone app, and doesn’t happen in the game editor.

1 Like