3

I'm trying to setup a simple hide and unhide gameobject in Unity, but can't seem to get my coding to work correctly. What wrong with what I have written so far.

#pragma strict

public var myObject :GameObject;

    if (GUI.Button(new Rect(1120,930,100,50),"3D MODEL"))

    {
        gameObject.SetActive(true);

    }


}
Coder
  • 499
  • 3
  • 13
  • 29
  • http://gamedev.stackexchange.com/ will maybe be a better place to ask (also check out http://answers.unity3d.com, the community here is fast and maybe know more about Unity3D than people from SO) ! Anyway, your code here is just to unhide the gameobject, right ? – Alexandre Beaudet Oct 14 '15 at 12:44

3 Answers3

3

There you go, this should solve your problem. http://answers.unity3d.com/questions/7776/how-to-make-an-gameobject-invisible-and-disappeare.html Good luck.

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;
Jorge Santos
  • 546
  • 1
  • 7
  • 17
0

Try this code instead of SetActive :

renderer.enabled = true; (to show the game object)

renderer.enabled = false; (to hide the game object)

If the gameobject got children in it, this piece of code might help :

function Hide() {
    if (#condition#) {
        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;
    }
}

( second code coming from : http://answers.unity3d.com/questions/14165/show-and-hide-a-prefab-or-gameobject.html)

Alexandre Beaudet
  • 2,774
  • 2
  • 20
  • 29
  • Yes, the gameobject does have children in it. maybe that the reason i can't hide and unhide gameobject. I can see the mesh renderer check box turn off, but mesh doesn't reappear. – Coder Oct 14 '15 at 12:52
  • Do you just want to hide the object or really set it inactive ? Those are not the same thing - Did the code I gave you worked ? – Alexandre Beaudet Oct 14 '15 at 12:54
  • I just wanted to hide and unhide gameobject. That it. – Coder Oct 14 '15 at 13:05
0

A GameObject is not running scripts once it is set un-active. Which inherently means it cannot run a script to set itself active again. an outside source must be what sets it active again.

maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37