unity enable and disable gameobject code example

Example 1: unity c# set gameobject active

gameObject.SetActive(true)

Example 2: unity enable gameobject

GameObject.Find("PlayMenu 3").SetActive(false);
// you could also store a GameObject as  variable

Example 3: how to disable a gameObject unity c#

public GameObject gameObj;//the gameobject you want to disable in the scene

        gameObj.SetActive(true); //set the object to active
        gameObj.SetActive(false);//set the object to disable

        gameObject.SetActive(true);//change the state of the current gameobject to active
        gameObject.SetActive(false);//change the state of the current gameobject to disable

Example 4: deactivate a gameobject unity

public void SetActive(bool value);

Example 5: on GAMEOBJECT enable unity

void OnEnable()
    {
        Debug.Log("PrintOnEnable: script was enabled");
    }