- Home /
Show and Hide a prefab or GameObject
Hi,
Does anyone have a script to show or hide a prefab (or game object) on a key press?
The prefab contains multiple objects which should all be shown or hidden at once.
thanks
david
if you mean to completely disable it: http://answers.unity3d.com/questions/35363/gameobjectactive-not-deactivating-all-children.html
Answer by duck · Mar 31, 2010 at 03:09 PM
You can't show/hide a prefab as such, because a prefab is like a "template" of an object which could exist in your scene. You probably want to hide/show a gameobject (which might or might not be an instance of a prefab).
Do do this, you set the GameObject's renderer.enabled property to either true or false, like this:
function Update() {
if (Input.GetKeyDown(KeyCode.Z)) {
// show
renderer.enabled = true;
}
if (Input.GetKeyDown(KeyCode.X)) {
// hide
renderer.enabled = false;
}
}
or, to toggle the visibility on and off with a single keypress:
function Update() {
if (Input.GetKeyDown(KeyCode.Z)) {
// toggle visibility:
renderer.enabled = !renderer.enabled;
}
}
If you need to change the visibility of a GameObject and its children, you can loop through every object and change each one, like this:
function Update() { if (Input.GetKeyDown(KeyCode.Z)) { ToggleVisibility(); } }
function ToggleVisibility() { // toggles the visibility of this gameobject and all it's children var renderers = gameObject.GetComponentsInChildren.(); for (var r : Renderer in renderers) { r.enabled = !r.enabled; } }
(moved the toggling code to a separate function, which is often a good idea once any particular section of code starts growing significantly in size)
Thanks for an extremely quick reply! those scripts work well for single meshes :)
however my prefabs have multiple meshes - how could i apply it to an entire instance of a prefab? [let me know if the proper etiquette on this site is to post a new question or not]
if the question is just a small addendum to the original question (like yours is), a comment is best. If it's more like "the next question" in your larger set of goals, a new question is best.
edited to include gameobjects with children (I'm guessing this is what you meant by 'multiple meshes', right?)
doh! accidentally dropped a bit of C# in there! Yes, changing 'bool' to 'var' will fix that. But also, the script assumed that the 'parent' gameobject had a renderer, wheras I'm guessing your's doesn't (only the children have them?). I have fixed that now too (which completely removes the need for that var anyway!) Duck 37 secs ago
ok - yeh I use c# for work, but tend to go with JS when helping out (if no preference is specified) because most c# people can convert from JS, but not the other way around! ;)
Answer by vbs · Jun 08, 2015 at 05:54 AM
For Unity 5, you access game object's rendered component with this syntax:
gameObject.GetComponent().enabled = false;
So the above answer would be:
function Update() {
if (Input.GetKeyDown(KeyCode.Z)) {
// show
// renderer.enabled = true;
gameObject.GetComponent<Renderer>().enabled = true;
}
if (Input.GetKeyDown(KeyCode.X)) {
// hide
// renderer.enabled = false;
gameObject.GetComponent<Renderer>().enabled = false;
}
}
For Unity 5.X this is the answer that works. The editor will recognize the old code as legacy and offer you to convert to the new syntax based on components.
Answer by Gardemarinas · Jun 23, 2012 at 10:17 PM
Whit C# to disable Textures I use this code:
public void ChangeMenuActivity(bool state)
{
ToggleVisibility(gameObject.transform, state);
}
void ToggleVisibility(Transform obj, bool state)
{
for (int i = 0; i < obj.GetChildCount(); i++)
{
if (obj.GetChild(i).guiTexture != null)
obj.GetChild(i).guiTexture.enabled = state;
if (obj.GetChild(i).guiText != null)
obj.GetChild(i).guiText.enabled = state;
if (obj.GetChild(i).GetChildCount() > 0)
{
ToggleVisibility(obj.GetChild(i), state);
}
}
}
You really should ins$$anonymous$$d adapt Ben's code into C# - it should perform much better. And, since I'm already criticizing your code, that's setting, not toggling visibility. Good na$$anonymous$$g can make a lot of difference.
public void Hidden(bool isHidden)
{
Renderer[] renderers = gameObject.GetComponentsInChildren<Renderer>();
foreach (Renderer r in renderers)
{
r.enabled = !isHidden;
}
}
This is the adaptation into c# that I use, and it works well.
Answer by JxDarkAngel · Dec 19, 2012 at 12:13 AM
Tambien se puede hacer de la siguiente forma:
using System.Reflection;
void OcultarPadreHijos(object objGO) {
PropertyInfo Propiedad= objGO.GetType().GetProperty("enabled");
Propiedad.SetValue(objGO,false,null);
}
Si quieres ocultar a sus componentes hijos, solo mandas a llamar a la función por cada hijo
Answer by gnoren · Apr 22, 2014 at 12:58 AM
Hey I know this is a pretty old thread, but I was hoping someone might have a bit of insight into a problem I'm having with this code (the last Toggle example) I am very new to Unity and am probably missing something obvious. I saved this as a script and childed it to the object (platforms) which I want to toggle:
But keep getting this error: Assets/Scripts/platforms_o_off.js(19,55): BCE0043: Unexpected token: ..
#pragma strict
function Start () {
}
function Update () {
if (Input.GetKeyDown(KeyCode.Z)) {
ToggleVisibility();
}
}
function ToggleVisibility() {
// toggles the visibility of this gameobject and all it's children
var renderers = gameObject.GetComponentsInChildren.(platforms);
for (var r : Renderer in renderers) {
r.enabled = !r.enabled;
}
}
hello I would like to know how come and go with the same key a model .fbx thank you very much
Your answer
Follow this Question
Related Questions
Zoom In: Reveal hidden object - help 3 Answers
Two exact objects behaving differently... *scratches head* 0 Answers
How to restart a "prefab'd" level? ;) 0 Answers
C# Child GameObject Disabled Script SetActive(false) 1 Answer
How to make an Instantiated prefab a Child of a gameobject that already exists in the hierarchy 3 Answers