- Home /
GUI Not updating
Hey all, I have had a look around and can not find a perfewct answer to my question. I am playing around with Unity again and think it is an amazing Game Engine. I am having a go at it with Javascript, I know C# but want to test my Javascript which is a little shoddy compared to other languages.
Anyhow, I want to use this script which I found when trying to find out an answer to my problem. However it is not updating the GUI at all! It just does nothing. I am using the standard controller which is included in the 2D tutorial.
var stringToEdit = "Player1";
var userHasHitOk : boolean = false;
function Update() {
if(Input.GetKeyDown("enter")) { userHasHitOk = true; }
}
function OnGUI () {
if (userHasHitOk)
{
GUI.Label (Rect (10, 10, 100, 30), "Hello " + stringToEdit);
}
else
{
GUI.Label (Rect (10, 10, 100, 30), "Enter Name:");
stringToEdit = GUI.TextField (Rect (90, 10, 200, 25), stringToEdit, 40);
}
}
This code is in the CameraFocus.js file but it just does not update at all. It displays the GUI but no update.
Hope someone can help,
Thank you
Well, if it displays at all that means it's updating! In what way isn't it working?
EDIT: Oh I see. Just a sec...
Sorry, when the user presses enter it does not update correctly with the username they have entered into the box.
Answer by syclamoth · Oct 10, 2011 at 09:44 AM
The problem here is that you are waiting for the user to press 'Enter' after they type some text into a text field. Unfortunately, the default behaviour for such fields is that they prevent normal Input.keyPress-type functions from working, so that the text field can be entered without the player accidentally using their input keys!
Recently the boolean value 'Input.eatKeyPressOnTextFieldFocus' has been added, which lets you override that behaviour. Just set
Input.eatKeyPressOnTextFieldFocus = false;
at the top of your script, and you'll be good to go!
Still nothing. Adding this to the top makes no difference.. Strange
That's odd. Does it work if you don't click the text field first?
No need to worry solved it. Enter seems to be looking for $$anonymous$$eypad Enter not Return.. Silly me. Changed this line to:
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return)) { userHasHitOk = true; }
Your answer
Follow this Question
Related Questions
turn off items from GUI 0 Answers
GUI.Window only shows for one frame under all circumstances 2 Answers
Weird GUI error (script included) 1 Answer
using GUI scrollView for multiple onGUI functions 0 Answers
GUI opacity. 1 Answer