Display fixed number of words in textbox at a time

Hi I have been trying to figure this out with no luck.

I am receiving text from an API and sometimes the text is received with over 10 words and sometimes only one word. I would like to write a script that will only allow up to 3 words to be displayed at a time in a text box on screen.

Then after 2 seconds it will move to the next three words. If there is less than three words it will only display the remaining words. Once it is finished I would like to call the API again to get more text. Can anyone help with this please?

I have been trying to create a lot of if statements but I am not sure how I would get the logic right? I think I might be overcomplicating this. Please help if there is an easier way. Thank you!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using JacobGames.SuperInvoke;

public class TesxtMeshAi : MonoBehaviour
    {
    int count = 0;
        public Text Ai;
        public string AiMesh;
    string CombText;
    private string[] TextBreak;
        public int timer;
    public GameObject Call; // the api to call
   
    public TextMeshProUGUI textDisplay;
    
        void Start()
        {
            AiMesh = Ai.text;
        textDisplay.text = AiMesh;
       TextChange();
   
    }
    public void TextChange()
    {
        AiMesh = Ai.text;
        TextBreak = AiMesh.Split(char.Parse("_"));
        //  textDisplay.text = AiMesh;
        count = 0;
       SuperInvoke.RunRepeat(3, 1, TextBreak.Length, RunText);
   
    void RunText()
    {


        if (count + 2 > TextBreak.Length)
        {
            CombText = TextBreak[count] + " " + TextBreak[count + 1];
             count = 0;
            Call.GetComponent<TestChatEasy>().Call();
         }
        if (count + 1 > TextBreak.Length)
        {
            Call.GetComponent<TestChatEasy>().Call();
        }

        if (count > TextBreak.Length) //was old one
        {
            count = 0;
            Call.GetComponent<TestChatEasy>().Call();
         
        }
        if (count >= 0)
        {
            if (TextBreak.Length < 2)
            {
                CombText = TextBreak[count] + " " + TextBreak[count + 1];
                textDisplay.text = CombText;
                count = 0;
                Call.GetComponent<TestChatEasy>().Call();
               //   TextChange();
            }
            if (TextBreak.Length < 1)
            {
                CombText = TextBreak[count];
                textDisplay.text = CombText;
                count = 0;
                Call.GetComponent<TestChatEasy>().Call();
              //   TextChange();
            }
            if (TextBreak.Length > 3)
            {
                count = count + 1;
                CombText = TextBreak[count] + " " + TextBreak[count + 1] + " " + TextBreak[count + 2];
                textDisplay.text = CombText;
            }

        }
    }

I am assuming your Call.GetComponent<TestChatEasy>().Call(); updates Ai.text and calls the TextChange() function. In which case I’d do this using a coroutine.

  public void TextChange() {
    StartCoroutine(RunText(Ai.text));
  }
  
  
  IEnumerator RunText(string words) {
    string[] word = words.Split('_');
    string output;
    int idx = 0;
    while (idx < word.Length) {
      // The condition above ensures that the first word is available.
      output = word[idx++];
      for (int i = 0; i < 2; i++) {
        // Break out if we run out of words, else add them on.
        if (idx >= word.Length) { break; }
        output += " " + word[idx++];
      }
      // Set the text.
      textDisplay.text = output;
      // Wait for 2 seconds
      yield return new WaitForSeconds(2);
    }
    // When we reach here we've finished with the words get some more.
    Call.GetComponent<TestChatEasy>().Call();
  }