x


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

more ▼

asked Mar 31 '10 at 03:02 PM

David 3 gravatar image

David 3
624 13 19 29

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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)

more ▼

answered Mar 31 '10 at 03:09 PM

duck gravatar image

duck ♦♦
40.9k 92 148 415

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]

Mar 31 '10 at 03:29 PM David 3

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.

Mar 31 '10 at 03:42 PM duck ♦♦

edited to include gameobjects with children (I'm guessing this is what you meant by 'multiple meshes', right?)

Mar 31 '10 at 03:47 PM duck ♦♦

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

Mar 31 '10 at 04:28 PM duck ♦♦

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

Mar 31 '10 at 04:29 PM duck ♦♦
(comments are locked)
10|3000 characters needed characters left

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);
        }
    }
}
more ▼

answered Jun 23 '12 at 10:17 PM

Gardemarinas gravatar image

Gardemarinas
0

You really should instead 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 naming can make a lot of difference.

Jun 25 '12 at 04:34 PM Cawas
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Dec 19 '12 at 12:13 AM

JxDarkAngel gravatar image

JxDarkAngel
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5054
x2074
x1250
x183
x64

asked: Mar 31 '10 at 03:02 PM

Seen: 31597 times

Last Updated: Dec 19 '12 at 12:13 AM