- Home /
Problems with simple dialogue
Been trying to get this bit of code working. I have it attached to empty game object that's a child of the "Main Camera". The idea is to have a GUI box that is filled with a string that contains the text of the player you're talking to. Then the player can select smaller buttons beneath it that will change the text according to what is said. My problem is that the string in GUI.Box text starts as off as "Thank You." instead of "Hello There!". Not sure of the issue, would love a resolution (preferably in javascript). Thanks.
#pragma strict
var Hello : String = "Hello there!";
function OnGUI ()
{
GUI.Box(Rect(0,Screen.height-150,Screen.width,50), Hello );
if (GUI.Button ( Rect (20,Screen.height-90,225,50), "Good Day."));
{
Hello = "Thank you.";
}
}
This line:
if (GUI.Button ( Rect (20,Screen.height-90,225,50), "Good Day."));
See the semicolon at the end of that line? Get rid of it.
Explanation: the semicolon "ends" the if
statement, which means it no longer controls whatever block of code is after it. In rare cases, experienced programmers actually want that behaviour, but for now it's usually best to assume that semicolons should never, ever go directly after loops or conditionals.
Don't feel silly about missing that, either. That has to be one of the worst gotchas in today's program$$anonymous$$g languages.
I'm hesitant to let this through the moderation queue because it's a frequently asked question. It's not a bad question, we're just trying to cut down on the number of duplicate threads in this area of the site. If you have anything else to add, feel free to comment here; if not, I'll drop the question from the queue in a little while.
If you can't find help, here, you can always try the forums or look up some tutorials online.
Your answer
Follow this Question
Related Questions
changing GUI Button text with a string array 2 Answers
How do I set a GUI button's text using a string from another script? 0 Answers
Create GUI based on an array 2 Answers
Remove GUI Box Drawing? 1 Answer
GUI.Button on MouseHover 1 Answer