Hey everyone. I’m new to scripting and Unity, and I could use some help.
For a class I have, I’m required to make a game in Unity, and I chose to do it in a GUI type style.
So far, this is what I have…
What I want to happen is when you click the Cactus button, a box gets displayed either to the right, or below the buttons. Then clicking the Flower button would do the same, but a different box, and so on.
I’m unsure as how to code this. Can anyone help? I’d appreciate it greatly! :
GUI buttons can be used in an if condition
if(GUI.Button(Rect,Texture))
then if someone clicks on that button you can create some other GUI element. For a simple texture below, you can use GUI DrawTexture.
(A) GUI.Button(Rect(x,y,witdh,height), /) is a function that returns a boolean value == true, each time you press the button and then again becomes false. (Its like being active false…so, each time u press the button it gives a true pulse!)
(B) Just like Update(), the OnGUI function is executed each frame.
so the problem is that the moment u press the button the label you want gets rendered but in the very next moment(frame) it dissappears when the button value returns to false…
try ‘enabling’ ur desired label at button press…
Here is my script that worked for me and i think there is probably an easier way to do it with GUI.text but this works as well:
var button1clicked=false;
function OnGUI () {
if(GUI.Button(Rect(50,50,50,50),"test"))//instead of the text "test" you can add your texture
{
if(!button1clicked)
button1clicked=true;
else
button1clicked=false;
}
if(button1clicked)
{
GUI.Box(Rect(100,100,100,100),"");
GUI.Label(Rect(110,110,90,90),"Testtext");
}
}
I tried it myself and it worked. This is the structure for one button.
Hope this works now if it doesn’t let me know and i try what i can do.