I have a Typewriter script attached to the text, which is a child of a button.
I want this script to run only if the button is interactable.
I made a public variable to store the button and used GetComponent() method, but it doesn't work.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
// attach to UI Text component (with the full text already there)
public class UITextTypeWriter : MonoBehaviour {
public Text txt;
string story;
public Button ParrentButton;
void Awake()
{
txt = GetComponent<Text>();
story = txt.text;
txt.text = "";
Button b = ParrentButton.GetComponent<Button>();
if (b.interactable)
{
StartCoroutine("PlayText");
}
}
IEnumerator PlayText()
{
foreach (char c in story)
{
txt.text += c;
yield return new WaitForSeconds(0.125f);
}
}
}
Where is my mistake? I would be grateful for your help!
Thank you in advance!