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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

gameObject.GetComponentsInChildren<TextMeshProUGUI>() is showing as an array

Discussion in 'Scripting' started by aswii200, Jul 31, 2022.

  1. aswii200

    aswii200

    Joined:
    Jul 24, 2022
    Posts:
    7
    Hi all,
    I'm working on a visual novel game and trying to add the choice buttons.

    The script here, I'm trying to link a "TextMeshProUGUI" with the GameObject so the choices text will be updated according to inkscript.

    But when I tried GetComponent, the error states "cannot implicitly convert TextMeshProUGUI[] to TextMeshProUGUI"

    some reason this GetComponent is outputting as an array instead of a single entity.

    here is a part of my script that only includes the error part and the initialising part.

    Code (CSharp):
    1. [Header("Choices UI")]
    2.     [SerializeField] private GameObject[] choices;
    3.     private TextMeshProUGUI[] choiceText;

    Code (CSharp):
    1.  void Start()
    2.     {
    3.         LoadStory();
    4.         choiceText = new TextMeshProUGUI[choices.Length];
    5.         Debug.Log(choiceText.Length);
    6.         Debug.Log(choices.Length);
    7.         int index = 0;
    8.         foreach(GameObject choice in choices)
    9.         {
    10.            choiceText[index] = choice.GetComponentsInChildren<TextMeshProUGUI>(); //error line
    11.             index++;
    12.         }
    13.         Debug.Log(choiceText.Length);
    14.     }
    is there any way to rectify this ?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    There are two things. GetComponentsInChildren, and GetComponentInChildren. They return different things.
     
    spiney199 likes this.
  3. aswii200

    aswii200

    Joined:
    Jul 24, 2022
    Posts:
    7
    My goodness, I've been a fool.

    Thanks, it works now.