How can I make the player start talking depending if there is a text on the screen ?

Before the game start all my ui texts are disabled. They are all TextMeshPro types.

173351-o9apw.jpg

Then when I’m running the game in one of my scripts I’m activating only the Scene Image that also activating his child Scene Text.

Then a text is show in the game for 10 seconds and with script the player is talking : I’m checking if one of the texts is not empty and enabled :

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

public class BlendShapesController : MonoBehaviour
{
    public TextMeshProUGUI[] texts;
    public bool startTalking = false;
    public float talkTime;
    public float duration;
    [Range(0, 100)]
    public float valueRange;
    private SkinnedMeshRenderer bodySkinnedMeshRenderer;
    private bool isTalking = true;

    // Start is called before the first frame update
    void Start()
    {
        bodySkinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        StartTalking();

        if (startTalking && isTalking)
        {
            StartCoroutine(AnimateMouth());
            StartCoroutine(TalkTime());

            isTalking = false;
        }

        if (startTalking == false && isTalking == false)
        {
            isTalking = true;
        }
    }

    //Lerp between startValue and endValue over 'duration' seconds
    private IEnumerator LerpShape(float startValue, float endValue, float duration)
    {
        float elapsed = 0;
        while (elapsed < duration)
        {
            elapsed += Time.deltaTime;
            float value = Mathf.Lerp(startValue, endValue, elapsed / duration);
            bodySkinnedMeshRenderer.SetBlendShapeWeight(0, value);
            yield return null;
        }
    }

    //animate open and closed, then repeat
    public IEnumerator AnimateMouth()
    {
        while (startTalking == true)
        {
            yield return StartCoroutine(LerpShape(0, valueRange, duration));
            yield return StartCoroutine(LerpShape(valueRange, 0, duration));
        }
    }

    public IEnumerator TalkTime()
    {
        yield return new WaitForSeconds(talkTime);

        startTalking = false;
    }

    public void StartTalking()
    {
        for (int i = 0; i < texts.Length; i++)
        {
            if (texts_.text != "" && texts*.transform.parent.gameObject.activeInHierarchy == true)*_

{
startTalking = true;
}
}
}
}
Then after 10 seconds in another script I’m disabling the Scene Image and the Scene Text is also get not active.
So now both the Image and the Text are not active like before running the game. But for some reason it keep getting inside in the StartTalking method and set the flag to true :
public void StartTalking()
{
for (int i = 0; i < texts.Length; i++)
{
if (texts_.text != “” && texts*.transform.parent.gameObject.activeInHierarchy == true)
{
startTalking = true;
}
}
}*

All the texts now are not activated one of them is not empty but they are all not activated and still it’s getting inside and set the startTalking flag to true.
texts are TextMeshProUGUI types so I wonder why it’s setting the flag to true if they are disabled ?
I used a break point in the StartTalking method and I see that the activeInHierarchy flag is false but it’s still getting inside and setting the flag to true.
I’m also not sure about the rest of the script code if I didn’t mess all the other flags.
Before all that my script was written in other way all the StartTalking method was not exist and I used to make a reference to this script from other scripts and to enable the variable startTalking setting it to true and everything was working fine. but I thought that maybe I can do it all in this script without the need to make a reference to it at all and to decide when the player should talk and when not. One script to control all the player talkings. My logic is that when there is a text on the screen from this 3 ui texts make the player talk._

Answering directly the title question and not the code shown by you, you could control the flow of start/stop talking with events.

When a text is activated to be shown on screen, fire an event that make you character move his mouth. When you turn the text off, fire another event that make the listeners stop talking.