How to slowly increase a button's alpha?

I am trying to make a button slowly appear after the coroutine, TypeLine, has finished. Am I better off coding, or creating an animation to do this? Here is the code I have so far:

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

public class IntroLines : MonoBehaviour
{
   public TextMeshProUGUI textComponent;
   public string[] BeginningLines;
   public float textSpeed;
   private int index;
   public Button IntroButton1;

   void Start()
   {    
      Button btn1 = IntroButton1.GetComponent<Button>();
      btn1.onClick.AddListener(DisplayNextLine);         
      textComponent.text = string.Empty;
      startIntroLines();
   }

   void DisplayNextLine()
   {
      if(textComponent.text == BeginningLines[index])
         {
            NextIntroLine();
         }
         else
         {
            StopAllCoroutines();
            textComponent.text = BeginningLines[index];
         }
   }

  
   void startIntroLines()
   {
      index = 0;
      StartCoroutine(TypeLine());

     
   }

   IEnumerator TypeLine()
   {
      foreach (char c in BeginningLines[index].ToCharArray())
      {
         textComponent.text += c;
         yield return new WaitForSeconds (textSpeed);
      }    
   }

   void NextIntroLine()
   {
      if (index < BeginningLines.Length - 1)
      {
         index++;
         textComponent.text = string.Empty;
         StartCoroutine(TypeLine());
      }
      else
      {
         //gameObject.SetActive(false);
      }
   }
  
}

You have this post tagged as “Resolved.” If you solved it yourself, it’d be cool to share your solution for other people who might be looking to do the same thing as you. If you haven’t got a solution yet, you might want to remove that tag on the post so people don’t pass on by, thinking you no longer need help.

2 Likes