0
\$\begingroup\$
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BlendShapesController : MonoBehaviour
{
    public Text[] 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[i].text != "")
            {
                startTalking = true;
            }
        }
    }
}

I want that if one of the ui texts is not empty and not disabled to start talking by calling the StartTalking method. The game start when all the ui texts are disabled one of them is not empty but they are all disabled. And during the game they are turning disabled enabled in the game and get empty not empty. On the ui text is not empty.

The problem is in the Inspector I can't drag disabled ui texts to the array of the script.

All the ui texts are childs under their own parents that are childs of the prefab name Game UI Texts.

ui texts

This are the ui texts in the screenshot that should be dragged to the sccript on the right in the Inspector :

Scene Text , Description Text , and Long Distance Description Text

The main goal is to use one script to control automatic the talking with the text in the game. When there is a text in the game on the screen StartTalking and stop talking when there is no text on the screen.

\$\endgroup\$
2
  • \$\begingroup\$ Opps my mistake it's not regular Text ui but it's TextMeshPro - Text (UI) type. What should I do then now ? What to change in my script ? \$\endgroup\$
    – Daniel Lip
    Commented Dec 30, 2020 at 18:06
  • \$\begingroup\$ First you need to change the 0 in blendshapescontroller script in the editor to 3. If you still can't drag and drop after that, you need to change the type of text to the one used by textmeshpro, which seems to be TextMeshPro \$\endgroup\$
    – Zibelas
    Commented Dec 30, 2020 at 18:39

2 Answers 2

1
\$\begingroup\$

If your current Text[] is still 0 as in the screenshot, you need to first increase the size of it to the amount you want to drag -> 3. You can drag them regardless if they are enabled or not.

Now you should be able to drag each of the texts into the text array. There is no need to change the type of the Array to a GameObject Array, Unity is able to find the matching component as long as there is only one Text component on the dragged GameObject.

\$\endgroup\$
1
  • \$\begingroup\$ Opps my mistake it's not regular Text ui but it's TextMeshPro - Text (UI) type. What should I do then now ? What to change in my script ? \$\endgroup\$
    – Daniel Lip
    Commented Dec 30, 2020 at 18:06
0
\$\begingroup\$

The array is of type Text, which is the type of a component, but you're trying to drag an object which contains the Text component, which is of type GameObject. Make your array of type GameObject[] and then use GetComponent or GetComponentsInChildren on the stored objects to get to the text itself.

EDIT: given the new info, I've included the import and type of the array you need:

using TMPro;

and then define the array as:

public TextMeshProUGUI[] texts;
\$\endgroup\$
4
  • 2
    \$\begingroup\$ Unity is able to handle this as long as there is only one text component on the GameObject which seems to be the case here. \$\endgroup\$
    – Zibelas
    Commented Dec 30, 2020 at 9:49
  • \$\begingroup\$ TIL thanks for the info \$\endgroup\$ Commented Dec 30, 2020 at 9:57
  • \$\begingroup\$ @ChrisTrott Opps my mistake it's not regular Text ui but it's TextMeshPro - Text (UI) type. What should I do then now ? What to change in my script ? \$\endgroup\$
    – Daniel Lip
    Commented Dec 30, 2020 at 18:06
  • \$\begingroup\$ @ShamenRaze Ah, in that case, you can add the import, and then the type for it is TextMeshProUGUI. I'll update my answer so you can see the code properly \$\endgroup\$ Commented Dec 31, 2020 at 5:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .