Toggle gameobject on/off with button

Hi, I’ve been trying to toggle a gameobject on and off with a button but keep getting errors like…

“No appropriate version of
‘UnityEngine.GUI.Button’ for the
argument list ‘(UnityEngine.Rect,
boolean, String)’ was found”.

Here is my current code, can anyone help?

var icon : Texture2D;
var toggleBool = true;
var target : GameObject;
 
function OnGUI () {
    GUI.Box (Rect (0,0,100,90), "Top-left");
    toggleBool = GUI.Button (Rect (10,30,90,20), toggleBool, "turn on/off");
    target.SetActive(toggleBool);
   }

or:

gameObject.SetActive(!gameObject.activeInHierarchy);

That’s not how GUI.Button works - no version takes a bool argument. Check out the docs.

You need to do something like

if(GUI.Button(Rect (10,30,90,20), "turn on/off"))
    toggleBool = !toggleBool
target.SetActive(toggleBool);

A method like this will work:

public GameObject GameObjectToHide;

 void Start()
    {
       GameObjectToHide = GameObject.FindGameObjectWithTag("GameObjectToHide");
    }

public void HideGameObject()

    {

        if 
            (GameObjectToHide.activeInHierarchy)
        {
            GameObjectToHide.SetActive(false);
        }
        else
        {
            GameObjectToHide.SetActive(true);
        }
    }