How to make an gameObject invisible and disappeared

I have 2 questions:

  1. How to make an gameObject invisible but I can still interact with it.
  2. How to make an gameObject disappeared and reappeared without destroying and creating a new object.

Even better than messing with the alpha or the renderer, is this solution which I’m now using without adverse side effects:

// Hide button
GameObject.Find ("ShareButton").transform.localScale = new Vector3(0, 0, 0);

// Show button
GameObject.Find ("ShareButton").transform.localScale = new Vector3(1, 1, 1);

You can turn off the rendering of a GameObject by disabling its MeshRenderer component, e.g.

GetComponent(MeshRenderer).enabled = false;

You can disable a GameObject entirely by making it inactive, e.g.

gameObject.active = false;

Just to answer your question regarding physics..

`renderer.enabled` is different to `renderer.gameObject.active` in that `.enabled` will continue to consider physics on your invisible object.

i have a question, if i set gameObject.active = false; does it stop the engine from making calculations (like physics ones) ?

I am preallocating 10 objects and placing them out of screen, but i don't want them to generate any collision/calculation,etc when i have them in this "not used" state.

Would gameObject.active = false; achieve this?

Thanks

I am trying to call this function with my current script but it is not working.

The game is third person, however this script is used to move the camera to first person view (to look at something closely for example) when the “F” key is depressed.

However the character mesh gets in the way so id like to turn it off using the above function, but it does not work. What am I doing wrong?

   var FPSLocation = GameObject.Find("Player/FirstPersonLocation");

function Update (){


if (Input.GetKey (KeyCode.F)){


GetComponent("RPGCamera").enabled = false;

GameObject.Find("CameraHolder/Camera");
transform.position = FPSLocation.transform.position;

GetComponent("Player/3rd Person Controller").enabled = false;



}

if (Input.GetKeyUp (KeyCode.F)){


GetComponent("RPGCamera").enabled = true;
GetComponent("Player/3rd Person Controller").enabled = true;

}
}

maybe set renderer.material.color.a = 0 to set alpha to 0, then you could even put the camera behind or inside the character and view from inside that mesh at like .25 opacity, depending on how cartoony youre going this could like kinda sweet

For unity 5 use this:

//Invisible
this.gameObject.SetActive(false);

//visible
this.gameObject.SetActive(true);

If it is UI object, I recommend using CanvasGroup component, set canvasGroup.alpha to 0 to make it invisible. This will keep the scripts on the object still running, even if it’s invisible, which GameObject.SetActive(false) won’t do.