- Home /
How to make an gameObject invisible and disappeared
I have 2 questions:
- How to make an gameObject invisible but I can still interact with it.
- How to make an gameObject disappeared and reappeared without destroying and creating a new object.
Answer by $$anonymous$$ · Nov 14, 2009 at 08:41 PM
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.Get$$anonymous$$eyUp ($$anonymous$$eyCode.F)){ $$anonymous$$ainCamera.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
Hi technicat! Two small additions to these good answers
"gameObject.active = false" does not stop the object from rendering.
"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.
Answer by illustir · Oct 21, 2015 at 02:31 PM
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...
Actually just figure this out, I'm using new Vector3(0, 1, 0), so that the y won't be hurt and it works!
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
Answer by Mann1ng · Oct 20, 2010 at 11:39 AM
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.
Answer by pablo · Nov 19, 2009 at 11:32 AM
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.
Answer by E2R_Ben · May 17, 2012 at 02:33 PM
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;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613161425im_/https://answers.unity.com/themes/thub/images/avi.jpg)