- Home /
GUI Button scripting
I have an issue about GUI buttons... What I want is to activate/deactivate a gameobject using a button by script. So first I created a button in the editor which comes with a "Button (Script)" section which have an "OnClick ()" section to put the actions. Then I drag the button to the empty field and attach my function:
GameObject ort;
void Start ()
{
ort = GameObject.Find ("Body Parts (HouseHolder)");
}
public void OnGUI()
{
if (ort.activeSelf == true)
{
if (GUI.Button (new Rect (15, 15, 100, 50), "House Holder"))
{
ort.SetActive (false);
}
}
if (ort.activeSelf == false)
{
if (GUI.Button (new Rect (15, 15, 100, 50), "House Holder"))
{
ort.SetActive (true);
}
}
}
Ok this does what I want but this creates a new button and I don't want that. I want to do this with the button that I originally created but I don't know how to call it!
This is because you are confusing the two different UI systems. the button you created is a button from the Ui system introduced in 4.6, while the whole OnGUI thing is the old way of doing things. For more info on the new buttons(and the new ui system) check this: https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button
Thanks, didn't get there was a difference by reading through other comments. I managed to do the code so here it goes:
public void Switch()
{
if (gameObject.activeSelf == true)
{
gameObject.SetActive (false);
} else
{
gameObject.SetActive (true);
}
}