- Home /
How to display GUI in sequence in trigger
Here is what I'm attempting to do, in gameplay description:
Player moves into trigger collider.
Player presses a button (Space) --> A GUI is displayed.
Player presses button again --> Second GUI is displayed.
Player presses button again --> Third GUI is displayed.
Player exits trigger collider --> GUI disabled.
So far, I've been able to cobble together a java code that is really very close to doing what I would like it to do. However, I can't figure out, yet, how to allow the player to press the same button (ie Space) in order to flip through the GUI "pages." This is, probably, because I'm using the function Update code. I'm very new at this, so hopefully this isn't a painfully obvious question.
Here's my code as it exists now:
#pragma strict
var Texture1 : GUITexture;
var Texture2 : GUITexture;
var Texture3 : GUITexture;
function OnTriggerStay()
{
if(Input.GetKeyDown( KeyCode.Space ))
{
Texture1.enabled = true;
Texture2.enabled = false;
Texture3.enabled = false;
}
}
function Update()
{
if(Texture1.enabled == true)
{
if(Input.GetKeyDown( KeyCode.L ))
{
Texture1.enabled = false;
Texture2.enabled = true;
Texture3.enabled = false;
}
}
if(Texture2.enabled == true)
{
if(Input.GetKeyDown( KeyCode.K ))
{
Texture1.enabled = false;
Texture2.enabled = false;
Texture3.enabled = true;
}
}
}
function OnTriggerExit(){
Texture1.enabled = false;
Texture2.enabled = false;
Texture3.enabled = false;
}
Thanks, any advice is greatly appreciated!
Thanks for your suggestion --
I'm not sure it's solving the problem, however -- with the break command, the inspector pops up with a notification that there is no loop in place to break from -- perhaps it needs a loop to cycle the textureNumber? not sure -- if I remove "break" it will successfully bring up the first GUI, but no others.
Answer by UniqueDragutin · Aug 29, 2014 at 03:42 AM
Check once if player press space then check which texture is enabled(first check texture 1 then if that texture is not enabled check second texture and so on)..
if(Input.GetKeyDown(Keycode.Space)){
if(Texture1.enabled){
}
else if(Texture2.enabled){
}
}
Hope I helped.
Answer by Maui-M · Aug 28, 2014 at 10:02 PM
You should be able to get what your looking for with the following code. You could also look into doing it with List as well.
var Texture1 : GUITexture;
var Texture2 : GUITexture;
var Texture3 : GUITexture;
var textureNumber : int = 1;
function OnTriggerStay()
{
if(Input.GetKeyDown( KeyCode.Space ))
{
Texture1.enabled = false;
Texture2.enabled = false;
Texture3.enabled = false;
if(textureNumber == 1)
Texture1.enabled = true;
else if(textureNumber == 2)
Texture2.enabled = true;
else if(textureNumber == 3)
Texture3.enabled = true;
else
break;
textureNumber++;
}
}
function OnTriggerExit(){
textureNumber = 1;
Texture1.enabled = false;
Texture2.enabled = false;
Texture3.enabled = false;
}
Thanks for this -- I was feeling like I had figured out a different solution, as I posted below -- my solution hasnt been very reliable though, and has been calling up GUIs out of sequence, so I wanted to try your script -- I'm running into this error though: "No enclosing loop out of which to break or continue."
Any thoughts?
Thanks much
Answer by dglendening · Aug 29, 2014 at 12:44 AM
Nevermind team, I think I figured it out. Here's what I came up with:
var Texture1 : GUITexture;
var Texture2 : GUITexture;
var Texture3 : GUITexture;
function OnTriggerStay()
{
if(Texture1.enabled == false && Texture2.enabled == false && Texture3.enabled == false && Input.GetKeyDown( KeyCode.Space ))
{
Texture1.enabled = true;
Texture2.enabled = false;
Texture3.enabled = false;
}
else if(Texture1.enabled == true && Texture2.enabled == false && Texture3.enabled == false && Input.GetKeyDown( KeyCode.Space ))
{
Texture1.enabled = false;
Texture2.enabled = true;
Texture3.enabled = false;
}
else if(Texture1.enabled == false && Texture2.enabled == true && Texture3.enabled == false && Input.GetKeyDown( KeyCode.Space ))
{
Texture1.enabled = false;
Texture2.enabled = false;
Texture3.enabled = true;
}
else if(Texture1.enabled == false && Texture2.enabled == false && Texture3.enabled == true && Input.GetKeyDown( KeyCode.Space ))
{
Texture1.enabled = false;
Texture2.enabled = false;
Texture3.enabled = false;
}
}
function OnTriggerExit(){
Texture1.enabled = false;
Texture2.enabled = false;
Texture3.enabled = false;
}
This isn't totally working, though -- for some reason, when testing the gameplay, GUIs get called up out of sequence. Not sure what the deal is.
Your answer
Follow this Question
Related Questions
Is it the same javascript 1 Answer
Script doesn't find other script 0 Answers
GUI animated menus 2 Answers
How to disable "GameObject" Then Enable After Animation. 1 Answer
oescape script name? 1 Answer