Hide/Unhide object in Unity3D via script

or azran picture or azran · Aug 31, 2014 · Viewed 18.9k times · Source

i want to set part of my scene to be hidden before everything starts, so lets say i have some isHidden (which im trying to find) property that i set to true before, or at the first frame of my scene.

at some point i want to unhide it and trigger the animation, i know how to trigger the animation. and the only way i found to hid object is to set it like :

SetActive(false);

it does hide is, but when i am trying to access it later in the script with

GameObject.Find("gameObjectName");

it returns null. so, what is the correct approach to hide or hide GameObject via script. this is not a Mesh so it dont have a mesh renderer.

Answer

Rudolfwm picture Rudolfwm · Aug 31, 2014

use Renderer.enabled

bool enabled;
Description

Makes the rendered 3D object visible if enabled.

example code:

public class ExampleClass : MonoBehaviour {
    void Show() {
        renderer.enabled = true;
    }
    void Hide() {
        renderer.enabled = false;
    }
    void HideChildren()
    {
         Renderer[] lChildRenderers=gameObject.GetComponentsInChildren<Renderer>();
         foreach ( Renderer lRenderer in lChildRenderers)
         {
              lRenderer.enabled=false;
         }
    }
    void ShowChildren()
    {
         Renderer[] lChildRenderers=gameObject.GetComponentsInChildren<Renderer>();
         foreach ( Renderer lRenderer in lChildRenderers)
         {
              lRenderer.enabled=true;
         }
    }


}