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

Turn off GUI in spawned Prefab

Discussion in 'Prefabs' started by DJgray3D, Apr 16, 2021.

  1. DJgray3D

    DJgray3D

    Joined:
    Mar 6, 2013
    Posts:
    15
    I have a scene with a button that spawns a Prefab.
    Within that Prefab is a 3D model and some GUI.
    I want to be able to turn off the prefabs GUI by pressing a button in the main scene but can't seem to get it working.
    I tried tagging the Prefab then referring to that in the script in order to find the GUI, but although I'm not getting an error message, its not turning the GUI off either
    This is the script I'm using:

    Code (CSharp):
    1. public GameObject[] buttons;
    2.  
    3. public void TurnOff(){
    4.     if (buttons == null)
    5.             buttons = GameObject.FindGameObjectsWithTag("InfoBox");
    6. foreach (GameObject respawn in buttons) {
    7.     respawn.GetComponentInChildren<CanvasGroup>().alpha = 0f;
    8.     }
    9. }
     
  2. DJgray3D

    DJgray3D

    Joined:
    Mar 6, 2013
    Posts:
    15
    I spend all day working on this and as soon as I post a question, I work it out! o_O

    For anyone else this might help, here's the solution:

    Code (CSharp):
    1. private GameObject buttons;
    2.  
    3. public TurnOff(){
    4. buttons = GameObject.FindWithTag("InfoBox");
    5. buttons.GetComponentInChildren<CanvasGroup>().alpha = 0f;
    6. }