Hide/Unhide object in Unity3D via script

…衆ロ難τιáo~ 提交于 2021-01-28 00:15:00

问题


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.


回答1:


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;
         }
    }


}


来源:https://stackoverflow.com/questions/25589344/hide-unhide-object-in-unity3d-via-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!