Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to make a GameObject and all of its children invisible via script?

Discussion in 'Scripting' started by R2D2BB8, Jul 7, 2023.

  1. R2D2BB8

    R2D2BB8

    Joined:
    Jun 13, 2023
    Posts:
    2
    Hi I have the following problem. I want to make a GameObject invisible when the user hits the return key, but everything I tried doesn't work. I tried looping through the children, but it just makes the first one invisible (I switched the order of the children to be sure, but everytime just the first one gets invisible) with this Code:
    Code (CSharp):
    1. public class textscript : MonoBehaviour
    2. {
    3.  
    4.     CanvasGroup m_CanvasGroup;
    5.     SpriteRenderer m_SpriteRenderer;
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.         m_CanvasGroup = GetComponent<CanvasGroup>();
    10.         m_SpriteRenderer = GetComponent<SpriteRenderer>();
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         if(Input.GetKey(KeyCode.Return)){
    17.             Hide();
    18.         }
    19.     }
    20.    
    21.     void Hide(){
    22.         m_CanvasGroup.alpha = 0f;
    23.         m_CanvasGroup.blocksRaycasts = false;
    24.        
    25.         var tempColor = m_SpriteRenderer.color;
    26.         tempColor.a = 0f;
    27.         m_SpriteRenderer.color = tempColor;
    28.        
    29.         GameObject Parent = this.gameObject;
    30.         int x;
    31.         for( x = Parent.transform.childCount; x > 0; x--);
    32.         {
    33.             Parent.transform.GetChild(x).GetComponent<SpriteRenderer>().color = tempColor;
    34.         }
    35.      }
    36.    
    37.      void Show(){
    38.         m_CanvasGroup.alpha = 1f;
    39.         m_CanvasGroup.blocksRaycasts = true;
    40.      }
    41. }
    I tried to use a MeshRenderer with this Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class textscript : MonoBehaviour
    6. {
    7.  
    8.     MeshRenderer m_MeshRenderer;
    9.     SpriteRenderer m_SpriteRenderer;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         m_MeshRenderer = GetComponent<MeshRenderer>();
    14.         m_SpriteRenderer = GetComponent<SpriteRenderer>();
    15.         Show();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if(Input.GetKey(KeyCode.Return)){
    22.             Hide();
    23.         }
    24.     }
    25.    
    26.     void Hide(){
    27.     m_MeshRenderer.enabled = false;
    28.      }
    29.    
    30.      void Show(){
    31.     m_MeshRenderer.enabled = true;
    32.      }
    33. }
    This didn't work either(Now nothing got invisible) . Here's my GameObject in the Prefab mode:
    upload_2023-7-7_16-35-49.png

    And I have run out of ideas to try. Can anyone help me?
    Thanks in advance for any help
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Easiest is always just to set the top GameObject to inactive.

    Code (csharp):
    1. myTopGameObjectReference.SetActive(false);
    If you want to blast through child Renderers and do it on a piece-by-piece basis, you would need to do a GetComponentsInChildren<T>() for each broad category of "thing" you want to find and set enabled false.

    I recommend the first approach if you can reorganize to tolerate the entire hierarchy being inactive.
     
    R2D2BB8 likes this.
  3. R2D2BB8

    R2D2BB8

    Joined:
    Jun 13, 2023
    Posts:
    2
    Thanks mate, it finally works!
     
    Kurt-Dekker likes this.