How to make a gameObject invisible?

Liam S-O picture Liam S-O · Feb 1, 2015 · Viewed 9.8k times · Source

I am attempting to make a Unity gameObject invisible through code. So far I have tried (On a script attached to the gameObject I want to make invisible): this.renderer.enabled = false this.enabled = false this.gameObject.enabled = false this.gameObject.renderer.enabled = false And on my player script: other.gameObject.enabled = false other.gameObject.renderer.enabled = false I have also tried Destroy (other.gameObject) but it doesn't achieve what I want from it.

Thanks for your help

Edit: Here is my code using the gameObject.SetActive command

    void Update () 
{
    if (BlueFlag.GotBlueFlag == false) 
    {
        print ("false");
        gameObject.SetActive(false);
    }
    if (BlueFlag.GotBlueFlag == true) 
    {
        gameObject.SetActive(true);
        print ("True");
    }
}

It works now, in part. However, it seems that it won't register if the GotBlueFlag bool on the BlueFlag script is true. If I use double "=" signs, It spams my "True" print statement. Ideas?

Another Edit: (Showing the BlueFlag script)

    void OnTriggerEnter (Collider other)
{
    if (other.collider.tag == "RedTank") 
    {
        print ("Flag");
        gameObject.SetActive (false);
        GotBlueFlag = true;
    } 

Answer

David picture David · Feb 1, 2015

Use below code:

gameObject.SetActive(false);

Documents here. Note that enabled is obsolete in the new Unity API!