Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.
Options

Access to gameobjects by name (Newbe needs help)

How I can get access to the game objects in a sceene by name in a script to enable / disable the object renderer (visible on/off).
A code example would be good.

Thanks!

Best Answer

Answers

  • Options

    @Schnappi

    If the script is attached to the gameobject in question, you can reference the gameobject with the "this" keyword:

    this.gameObject

    with that reference you can then turn on and off rendering with routines that enable and disable the gameObject's Renderer components:

     public void OnHide()
        {
            Renderer[] rendererArray = this.gameObject.GetComponentsInChildren<Renderer>();
            foreach (Renderer r in rendererArray)
            {
                r.enabled = false;
            }      
        }
    
        public void OnShow()
        {
            Renderer[] rendererArray = this.gameObject.GetComponentsInChildren<Renderer>();
            foreach (Renderer r in rendererArray)
            {
                r.enabled = true;
            }        
        }
    

    If the script is not attached to the game object that you want to show or hide and you don't have a reference to it already you can get a gameobject reference with GameObject.Find code like:

    GameObject targetGameObject;
    
     targetGameObject = gameObject.Find("TargetGameObjectsName");
    

    where the string "TargetGameObjectsName" is the name of the gameObject in the scene object hierarchy.

    Similar code to the show and hide routines could then use the targetGameObject reference to disable and enable the renderers for the game object.

    Alternatively you may also be interested in using the reference to just disable the gameObject with GameObject.SetActive but enabling and disabling the renderers is a better way to go if you want to control the gameObject's visibility.

    HTH.

    Windows Holographic User Group Redmond

    WinHUGR.org - - - - - - - - - - - - - - - - - - @WinHUGR
    WinHUGR YouTube Channel -- live streamed meetings

Sign In or Register to comment.