Home>

To create a List
I created the following Prefab, referred to the videoList, and poured it in For.

List
string FileNameNum (file name)
int QFlag (0: ButtonA display 1;ButtonB display)

ListPrefab
L Text
L Image
L ButtonA
L ButtonB

Text and Image were created as an array and displayed well with GetComponentInChildren.
Buttons A and B use SetActive if I want to display them depending on the conditions.
I have to refer to it in GameObject and I can't use GetComponentInChildren.
I checked there and it seems that I can refer to Button with child.gameObject ()
I wrote the following code.

for (int i = 0;i<_videoList.Count;i ++)
            {
                var item = GameObject.Instantiate (ListPrefab) as GameObject;
                item.transform.SetParent (transform, false);
                // Number Display
                item.GetComponentInChildren<Text>(). Text = (i + 1) .ToString ("D3");
                // Image Display
                images = item.GetComponentsInChildren<Image>();
                FileNameNum = _videoList [i] .FileNameNum;
                FilePath = Application.persistentDataPath + "/" + FileNameNum + ".png";
                images [1] .sprite = LoadSprite (FilePath);
                // Button Display
                QFlag = _videoList [i] .QFlag;
                buttonObject = item.child.gameObject ();
                switch (QFlag)
                {
                    case 0:
                    buttonObject [2] .SetActive (true);
                    buttonObject [3] .SetActive (false);
                    break;
                    case 1:
                    buttonObject [2] .SetActive (false);
                    buttonObject [3] .SetActive (true);
                    break;
                }
            }


However, I get the following error.
What should I do?

Assets \ Scripts \ VideoListDisplay.cs (35,37): error CS1061:'GameObject' does not contain a definition for'child' and no accessible extension method'child' accepting a first argument of type'GameObject' could be found (are you missing a using directive or an assembly reference?)


environment)
PC: Windows 10
Unity 2019.4.0f1
Language: C #

  • Answer # 1

    I have to refer to it in GameObject and I can't use GetComponentInChildren.

    No, you can.

      GameObject gObj = ~ .GetComponentInChildren<~>(). GameObject;

    About the error

      buttonObject = item.child.gameObject ();

    ofchild.gameObject ()Does not even exist, so I think you should fix it as follows.

      buttonObject = item.transform.GetChild (0) .gameObject;