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. Dismiss Notice

Question Change text of instanced button

Discussion in 'UI Toolkit' started by rogerdv, Oct 9, 2023.

  1. rogerdv

    rogerdv

    Joined:
    Jul 16, 2012
    Posts:
    88
    I need to instance several buttons and set the text of each one. I have tried several ways, from searchin the button and setting its text member (I get an error) to searching for some Text or text child but neither works.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,849
    Not sure why setting a button's
    .text
    property wouldn't work. It should be as simple as this:

    Code (CSharp):
    1. var button = new UnityEngine.UIElements.Button()
    2. {
    3.     text = "Some Text"
    4. };
    5. Add(button);
     
  3. rogerdv

    rogerdv

    Joined:
    Jul 16, 2012
    Posts:
    88
    Last night, after days of work, I found the solution. Cant remember what was it, but for some reason the instanced TemplateContainer Q<> wasnt returning the button.