- Home /
Showing and hiding guiText after a key is pressed
Here is the thing. First I'm showing an animation of a rotating cube, then after 4 seconds I display a speech ballon (Gui Label). The balloon should be visible until the user presses the Spacebar. After this another speech ballon is shown. Then again the user needs to press the Spacebar to hide it. In total I show 3 speech ballons and after that is finished I play an animation. Here is the code:
function Intro()
{
yield WaitForSeconds(4);
gameObject.Find("Cubo Text").SendMessage("Show",0); // This calls a function that displays the correct message
while (!Input.GetKey("space")) yield;
gameObject.Find("Cubo Text").SendMessage("Show",1);
while (!Input.GetKey("space")) yield;
gameObject.Find("Cubo Text").SendMessage("Show",2);
while (!Input.GetKey("space")) yield;
gameObject.Find("CuboB").animation.Play("roll");
}
This function is called in Start() . The problem is that I can see the first speech balloon, but after pressing the Spacebar I can't see any other baloon and the animation starts inmediatly. Any ideas why this happens and how to solve it?
Answer by Kleptomaniac · Feb 26, 2012 at 12:02 AM
Hey there!
This should not be called in Start(). Start is literally only called at the initialisation of a script, so at the beginning of runtime. After the start of runtime, it is not called again. Therefore after the very start of runtime, your key inputs aren't being received. That's what Update() is for!
I'd say go about you're script like this:
var balloonNumber : int = 0;
function Start() { cuboText = gameObject.Find("Cubo Text"); cuboB = gameObject.Find("CuboB"); }
function Update() {
InvokeRepeating("SpeechBubbles", 4, 0);
}
function SpeechBubbles() { cuboText.SendMessage("Show", balloonNumber); if (Input.GeyKeyDown(KeyCode.Space) && balloonNumber < 2) { balloonNumber++; } if (balloonNumber == 3) { cuboB.animation.Play("roll"); } }
See how you go with that. Klep
I just realised that you wanted to do the animation last. Edited the code to account for this and also changed a mistake which would have allowed for infinite presses of the spacebar. Good luck with your game!
Thank you very much for the answer. Yeah, after a longer inspection I too realized that the Get$$anonymous$$eyDown 's value was not updating 'cause it only runs once u__u. I tried your solution, however I still have issues. The speech bubbles are being shown for a fraction of second, but after that they dissapear (completely ignoring the request for the spacebar, which by the way had a small mistyping Input.Gey$$anonymous$$eyDown ins$$anonymous$$d of Input.Get$$anonymous$$eyDown :p). Do you know why???
Thanks for noticing my mistyping of Get$$anonymous$$eyDown. I'm editing the code with (hopefully) the final revisions. I think it should work. It's also a lot simpler now.
O$$anonymous$$, I edited. I don't think my revision seems to be updating so I'll just chuck it in this comment:
var balloonNumber : int = 0;
function Start() {
cuboText = gameObject.Find("Cubo Text");
cuboB = gameObject.Find("CuboB");
}
function Update() {
InvokeRepeating("SpeechBubbles", 4, 0);
}
function SpeechBubbles() {
cuboText.Send$$anonymous$$essage("Show", balloonNumber);
if (Input.Gey$$anonymous$$eyDown($$anonymous$$eyCode.Space) && balloonNumber < 2) {
balloonNumber++;
} if (balloonNumber == 3) {
cuboB.animation.Play("roll");
}
}
See how that goes. If it doesn't work, just tell me.
It works!!!!! I've been trying to solve this ting for over 2 days... <3 <3 <3
Before concluding this would you $$anonymous$$d explaining me how InvokeRepeating works????
Your answer
Follow this Question
Related Questions
Returning from and waiting for a Message function 0 Answers
How do I have Unity Wait for a keypress? 1 Answer
Change ctrl key unity editor collider 2D 0 Answers
Multiple key. Press order 0 Answers
Select object with mouse and hide its children with key, 2 Answers