Problem with coroutines (probably)

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

public class Pianist : MonoBehaviour {

    public static Pianist pp;
    Text box ;
    bool play, intoB ,bar;
    string txt;
    int index, size;
    float wait;
    // Use this for initialization
    void Start ()
    {
        if (pp == null)
        {
            pp = gameObject.GetComponent<Pianist>();
        }
        box = gameObject.GetComponent<Text>();
        play = false;
        index = 0;
        wait = 0.0f;
        StartCoroutine(PPiano());
    }
   

    IEnumerator PPiano()
    {
        while (true)
        {
            txt = box.text.ToString();
            size = txt.Length;
            if (play && (size > 0) && (index < size))
            {

                if (txt[index] == '[')
                {
                    intoB = true;
                }
                else if (txt[index] == ']')
                {
                    intoB = false;
                    wait = 0.2f;
                }
                else if (txt[index] == '|')
                {
                    bar = true;
                    wait = 0.5f;
                }
                else if (txt[index] == ' ')
                {
                    if (!bar)
                    {
                        if (intoB)
                        {
                            wait = 0.1f;
                        }
                        else
                        {
                            wait = 0.3f;
                        }
                    }
                }
                else if (char.IsLetterOrDigit(txt[index]) || char.IsSymbol(txt[index]))
                {
                    bar = false;
                    yield return new WaitForSeconds(wait);
                    InputSimulator.SimulateTextEntry(txt[index].ToString());
                    if (intoB)
                    {
                        wait = 0.0f;
                    }
                    else
                    {
                        wait = 0.2f;
                    }
                }
                index++;
            }
        }
    }


    public void SP(bool change)
    {
        play = change;
        return;
    }

    public void RP()
    {
        play = false;
        index = 0;
        wait = 0.0f;
    }
}

this is my code and i’ve been making an app that would read music sheet notes and simulate pressing them
,i know that it’s a game engine but i wanted to try this on unity.
any way i’m using a pre-made namespace that i downloaded to simulate key press ,
but i don’t think it is the problem because it doesn’t get there.
the moment i hit play Unity crashes.
any help ?

PPiano is stuck in an infinite loop. Add “yield return null;” just before the closing “}” of the “while (true){” loop to let other things on the Unity thread get processed and move to the next update cycle.

1 Like

Thank you!! It worked
Also thank you for explaining