Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Creating a Gameobject array from Buttons

Discussion in 'Scripting' started by DoomDude99, May 21, 2019.

  1. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    This question is related to:

    https://forum.unity.com/threads/changing-menu-layout-in-a-script.681583/

    with the code:

    Code (CSharp):
    1. public List<GameObject> StartObjects;  //Put all the GameObjects which are active when the game starts here
    2. public List <GameObject> AfterStartObjects;  //Put all the GameObjects which need to activate when the Start Game button is press here
    3. //In the inspector, deactivate all objects in the AfterStartObjects list
    4. //Have the Start Button call this method
    5. public void StartButtonPressed()
    6. {
    7.     //Deactivate all StartObjects
    8.     foreach (GameObject startObject in StartObjects)
    9.     {
    10.         startObject.SetActive(false);
    11.     }
    12.     //Activate all AfterStartObjects
    13.     foreach (GameObject afterStartObject in AfterStartObjects)
    14.     {
    15.         afterStartObject.SetActive(true);
    16.     }
    17.     //Add code here about actually starting the game, or whatever you want to happen when
    18.     //the player presses Start Game beyond just playing with buttons and the background
    19. }
    In the reply, it's suggested that the buttons are stored in GameObject arrays that are activated/deactivated as the user interacts with the app. The buttons are already created and present in the canvas. How may I populate the arrays that store them?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    They are public list. You attach the script to a Gameobject and the list will be there in the inspector to populate. You can drag and drop into them. That's the simplest way to set it up at least.
     
  3. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    Ok, I got it. Now, what's the best way to control what buttons appear when a scene is loaded?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Well...That depends on your design. You can group buttons together under a gameobject and just turn on what you need. You can keep them single and turn off and on what you need.

    Just need a script that determines what should be visible that sets things up when your scene loads. Since there are a variety of methods and layouts, there isn't necessary a best way.
     
    DoomDude99 likes this.
  5. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    I added the buttons to a GameObject array:



    (I have two arrays of GameObjects):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class CanvasScript : MonoBehaviour
    7. {
    8.     public List<GameObject> startButtonObjects;
    9.     public List<GameObject> ingameButtonObject;
    10.                                              
    11.    
    12. }
    Is there a way to hide/unhide buttons when the scene is loaded (the bottom two buttons mustn't be shown when the game starts).
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    If you already know they shouldn't be shown, just turn them off by default. If you need them on for some reason at the start, you can loop through in Awake or Start and turn them off using the code example you have above.
     
    DoomDude99 likes this.
  7. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    Now, unity can't seem to see methods in my script; I have a file CanvasScript.cs:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class CanvasScript : MonoBehaviour
    7. {
    8.     public List<GameObject> startButtonObjects;
    9.     public List<GameObject> ingameButtonObject;
    10.  
    11.     void Start() {
    12.         foreach (GameObject startObject in startButtonObjects)
    13.         {
    14.             startObject.SetActive(true);
    15.         }
    16.  
    17.         //Activate all AfterStartObjects
    18.         foreach (GameObject afterStartObject in ingameButtonObject)
    19.         {
    20.             afterStartObject.SetActive(false);
    21.         }
    22.     }
    23.  
    24.     public void StartButtonPressed()
    25.     {
    26.         foreach (GameObject startObject in startButtonObjects)
    27.         {
    28.             startObject.SetActive(false);
    29.         }
    30.  
    31.         //Activate all AfterStartObjects
    32.         foreach (GameObject afterStartObject in ingameButtonObject)
    33.         {
    34.             afterStartObject.SetActive(true);
    35.         }
    36.     }
    37.  
    38.     public void QuitButtonPressed()
    39.     {
    40.  
    41.     }
    42.  
    43.     public void ChangeScene()
    44.     {
    45.  
    46.     }
    47. }
    48.  
    and I attach it to the "StartButton" but the public methods don't seem accessible:

     
  8. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87