- Home /
Why are my GUI Buttons Triggering automaticaly
I'm trying to make a menu for my game using GUI buttons, I followed the instructions at :http://docs.unity3d.com/Manual/gui-Controls.html and put code in to be used once the button is clicked, the problem i'm having is that when i start my game it will open normally and after about half a second it will trigger the code for when I click the button, without me clicking them.
#pragma strict
private var Options : boolean;
var OptionsGUI : GameObject;
var MenuGUI : GameObject;
function OnGUI()
{
if(GUI.Button(Rect(175, 210, 400, 75),"O P T I O N S"));
{
SetOptions();
}
}
function SetOptions ()
{
//this code is what i'm using to switch between GUI i tested it and it works
OptionsGUI.GetComponent(options).enabled = true;
MenuGUI.GetComponent(menu).enabled = false;
}
Answer by archie_azares · Aug 23, 2014 at 11:35 AM
Instead of creating Set options functions within the script why don't you try to create another script containing the function then attach it to the gameobject, call the script attached to the gameobject and then call the function inside the if statement. because in the case of your script the function would automatically run every time the script executes. you may use Gameobject.Find("nameOfYourGameObject").GetComponent(name of script); to get the script.
Why make things more complex then the need to be? The structure of this code is fine. Problem is just a simple typo.
Your answer
Follow this Question
Related Questions
How to delete new'd arrays? 2 Answers
How to create random movement in 2D 2 Answers
Get all Game Objects near point 2 Answers
TextAsset inside the Array - UnityScript/JavaScript 1 Answer