Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by TimBur · Jul 11, 2015 at 05:25 AM · scripting problemuibutton

How to get an array of all Buttons attached to a Panel?

Given a Panel with some number of Buttons, I'd like to have a script on that Panel that can manipulate each Button in the panel, one-by-one, in a particular way. Unfortunately, I'm having big trouble with the very first step: getting an array of Buttons.

The scripting manual says that Button is descended from MonoBehavior, which suggests you ought to be able to treat a Button as a component. So I tried this:

 Button[] buttons = this.GetComponents<Button>();
 Debug.Log ("buttons.length = " + buttons.Length);

but the log then tells me that buttons.Length = 0;

The object heirarchy suggests that Buttons are GameObjects, as they have a transform, and can both be children and have children. So I tried this:

         int j=0;
         Button[] buttons = new Button[6];
         foreach(Transform child in transform)
         {
             if(child.gameObject is Button)
             {
                 buttons[j] = (Button) child.gameObject;
                 j++;
             }
             GameObject go = child.gameObject;
         }

but it fails to compile. Unity warns me that the expression (child.gameObject is Button) can never evaluate to true, and that the cast (Button) child.gameObject is illegal.

Any advice? Within a script attached to a Panel, how can you get an array of all Buttons that are children of that Panel?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by tauseef_xavi · Jul 11, 2015 at 06:47 AM

 Button[] buttons = this.GetComponentsInChildren<Button>();
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Jul 11, 2015 at 08:57 AM 1
Share

I'd also use the optional parameter that includes inactive components:

 Button[] buttons = this.GetComponentsInChildren<Button>(true);
avatar image TimBur · Jul 14, 2015 at 12:48 AM 0
Share

Both great answers.

In hindsight, I see how that makes sense: $$anonymous$$y Buttons are component-type objects, and they are also children of the Panel. Thanks.

avatar image
0

Answer by sas_88 · Jul 11, 2015 at 06:14 AM

Hi,

Follow the tutorial

https://www.youtube.com/watch?v=r1ALQCwR1Bs

Along with that,

Create a button to the panel in which size has to be displayed.

1.create a script attach it to the panel. 2.create a public gameobject and assign the button to it.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.Events;
 using UnityEngine.EventSystems;
 
 public class Create_Button : MonoBehaviour
 {
 
     public GameObject Base;
     
     GameObject[] Buttons=new GameObject[20];
 
         int count=18;
     
     void Start () 
     {
        cartdet ();
     }
     void cartdet () 
     {
           Buttons=new GameObject[count];
 
        Buttons[i]=Instantiate(Base, new Vector3(0f,0f,0.0f),           Quaternion.identity) as GameObject;
             Buttons[i].transform.SetParent (this.transform);
 
             Buttons[i].GetComponent<RectTransform>().localScale=new Vector3 (1f,1f,1f);
 
             Buttons[i].name="Button"+i.ToString();
         }
 }

The output of the above script will looks like

alt text


button-scroll.png (260.6 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UI Button - get reference to the parameter passed to function under OnClick() 1 Answer

[SOLVE] OnMouseEnter not working on UI elements 2 Answers

Pressing space calls the wrong function 1 Answer

UI Buttons to work continuously when pressed and held for sometime 1 Answer

Is there a way to have 1 Button update the values of variables of a script that is on multiple game objects? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges