- Home /
Cannot implicitly convert 'UnityEngine.UI.Button[]' to 'UnityEngine.UI.Button'
Basically i was following a tutorial and i checked if i did the right things heres my code
private void InitShop(){
//make sure we assigned the references
if (colorPanel == null || trailPanel == null){
Debug.Log ("You did not assign shop/trial color in the inspector");
}
//for each children transform under our color panel, find the button and add on-click
int x = 0;
foreach (Transform t in colorPanel) {
int currentIndex = x;
//error here
Button b = t.GetComponents<Button> ();
b.onClick.AddListener (() => onColorSelect (currentIndex));
x++;
}
Comment
Best Answer
Answer by Bunny83 · Jul 21, 2017 at 11:04 PM
You should be more careful. There's a method called GetComponents
and another one called GetComponent
. Notice the difference: plural and singular. "GetComponents" returns an array (possible multiple components) while "GetComponent" directly returns the first component of that type.
Your answer
![](https://koobas.hobune.stream/wayback/20220612130833im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to know if Unity UI button is being held down? 0 Answers
Add Listeners to array of Buttons 2 Answers
Unity - keep created buttons after quit 1 Answer
Change UI Buttons Focus Order 0 Answers