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 How to change the TextMesh component inside various buttons throught a script?

Discussion in 'UGUI & TextMesh Pro' started by MoryG65, Apr 14, 2023.

  1. MoryG65

    MoryG65

    Joined:
    Jan 19, 2023
    Posts:
    3
    sorry for my english

    i'm new to unity and i dont know what i'm doing wrong:

    i'm making a turn based game and i'm stuck loading the "fighter" move names(in my case mecha)
    into the buttons of the hud


    public void SetMoveNames(List<Button> moveButtons, List<Move> moves)
    {

    for (int i = 0; i < moveButtons.Count; i++)
    {
    if (i < moves.Count)
    {
    string moveName = moves.Base.Name;
    moveButtons.GetComponentInChildren<TextMeshProUGUI>().text = moveName;
    }
    else
    {
    moveButtons.GetComponentInChildren<TextMeshProUGUI>().text = "-";
    }

    }

    }

    this is the function inside my BattleDialogBox script that i use to update the button's text (that dont work lol)

    moveButtons is a list where i've dragged and dropped the textmesh component of the buttons into the exposed fields (like this "[SerializeField] private List<Button> moveButtons;" )

    and in my BattleSystem script ;



    public IEnumerator SetupBattle()
    {
    playerUnit.Setup();
    playerHud.SetData(playerUnit.Mecha);

    enemyUnit.Setup();
    enemyHud.SetData(enemyUnit.Mecha);

    dialogBox.SetMoveNames(dialogBox.GetMoveButtons(), playerUnit.Mecha.Moves);
    yield return dialogBox.TypeDialog("The players take their positions as the match gets started.");
    yield return new WaitForSeconds(1f);

    PlayerAction();
    }

    i update the buttons text with this line "dialogBox.SetMoveNames(dialogBox.GetMoveButtons(), playerUnit.Mecha.Moves);"

    what am i doing wrong ?