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.

8 Answers

8

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);

I love you man, this worked after wasting 1 hour with setActive (which I cannot bring back) and moving buttons into layers that I don't render

This is so simple, I didn't even consider it and yet it works like charm. Not to mention that disabling the mesh renderer is a bit tricky when u want a non-mesh parent to disappear with all of its children. Thank you.

This is great! However, I have difficulties with this implementation and ScrollRect. When changing the localScale to Vector3.One, the ScrollRect is always at the bottom. Any way to workaorund this? I want it to be at the top. Already tried ScrollRect.verticalNormalizedPosition = 1 with no success...

Your solution is the best. Thank you, man. Setting the meshrenderer true or false affect other gameobject with the same type. (Eg: if you set the capsule's meshrenderer to false all other capsules will be off even if you use tags).

This should be flagged as the best answer. Simple and effective

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;

where does this text go and what do i attach it to? i just get error if i put it into the update and attach it to an object.

you cannot use adjust a property on an uninstantiated object (e.g. a function call) GetComponent("RPGCamera").enabled = true; I would go into your RPGCamera script and generate some setters and getters. then you could say: if (Input.GetKeyUp (KeyCode.F)){ MainCamera.setEnabled = false; RPGCamera.setEnabled = true; }

@tensus2000 It is important that you use Unity 3. If you use Unity 4 you should use this script http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SetActive.html

ok, apparently you can... odd.

Hi technicat! Two small additions to these good answers 1. "gameObject.active = false" does not stop the object from rendering. 2. "gameObject.renderer.enabled = false" only works if you have one renderer on your object, at the top level. If you have a complex model with lots of parts and a hierarchy then one must hunt them all down and disable them all.

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.

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.

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

@pablo I think you should copy this question into a new question as you should only reply to the first question if you have the answer or a comment. Hope you will understand.

now its .enabled, but it will not do any calculations... no.

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);

This isn't what he meant. There are two differences from turning an object off and hiding it. Turning it On/Off (gameObject.SetActive(true/false) : Turns the object off when set to false, when set to true the object is turned on. The object will still be in the same location where is was when the object was set to false. If you attach a cube to a camera and have the player walk away, when you set the object to true, the cube will not be facing infront of the camera. It will be found where you set the object false and reture to the game after the object was set to true.