Home > database >  Unity C# - Trying to pause coroutine until a specific GameObject is disabled
Unity C# - Trying to pause coroutine until a specific GameObject is disabled

Time:04-21

I'm currently making a demo, and for my scenario I need to pause the coroutine until I make a specific movement in my demo that disables a specific GameObject. For that I tried to make a boolean function that will state if the object is active or not (with activeSelf) and in my coroutine main function, I made a while(is_active) loop with a yield return null inside (I show the code below). My problem is that the Is_Active boolean function seems to block and I don't gete any more messages when I make the Object active the first time(via the function ShowArrowAndOutline()).

Do you have any ideas or solutions that could help me, please ?

public class Scenario : MonoBehaviour
{

    public float delay = 0.1f;
    private string fullText = "Welcome to *** demo !";
    private string currentText = "";

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(ShowText());
    }


    IEnumerator ShowText()
    {
        yield return StartCoroutine(LetterbyLetter());
        yield return new WaitForSeconds(3);

        fullText = "We will guide you through this experience.";

        yield return StartCoroutine(LetterbyLetter());
        yield return new WaitForSeconds(3);
        
        fullText = "Let's start with the Steering Wheel !";

        yield return StartCoroutine(LetterbyLetter());
        yield return new WaitForSeconds(3);

        GameObject TextBox = GameObject.Find("TextBox");
        TextBox.SetActive(false);
        this.GetComponent<Text>().text = "";

        ShowArrowAndOutline();

        while(Is_Active()){
            yield return null;
        }

        
        GameObject m_Canvas = GameObject.Find("Canvas");
        TextBox = FindChildObject(m_Canvas,"TextBox");
        TextBox.SetActive(true);

        GameObject guideText = FindChildObject(TextBox,"GuideText");
        guideText.SetActive(true);
        Debug.Log("debug message");
        
    }


    IEnumerator LetterbyLetter()
    {
        for(int i = 0; i < fullText.Length 1; i  ){
            currentText = fullText.Substring(0,i);
            this.GetComponent<Text>().text = currentText;
            yield return new WaitForSeconds(delay);
        }
    }


    void ShowArrowAndOutline()
    {
        GameObject camera = GameObject.Find("Camera");
        GameObject arrow = FindChildObject(camera,"ArrowTarget");

        arrow.SetActive(true);
        GameObject steeringwheel = GameObject.Find("SteeringWheel");
        Outline outline = steeringwheel.GetComponent<Outline>();
        outline.enabled = true;
    }

    
    GameObject FindChildObject(GameObject parent, string child_name){
        Transform trs = parent.GetComponentInChildren<Transform>(true);
        GameObject child = new GameObject();
        foreach(Transform t_child in trs)
        {
            if(t_child.name == child_name){
                child = t_child.gameObject;
            }
        }
        return child;
    }

    bool Is_Active()
    {
        GameObject camera = GameObject.Find("Camera");
        GameObject arrow = FindChildObject(camera,"ArrowTarget");
        if(arrow.activeSelf == false){
            return false;
        }
        return true;
    }

    void Update()
    {
        Debug.Log(Is_Active());
    }
}

CodePudding user response:

You could give the object you're waiting to get disabled (the ArrowTarget) a script with an OnDisable() method (When the arrow is Disable

When the arrow is Active

  • Related