Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Problem with coroutines (probably)

Discussion in 'Scripting' started by SirMabrouk, Jul 29, 2017.

  1. SirMabrouk

    SirMabrouk

    Joined:
    Jul 28, 2017
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using WindowsInput;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class Pianist : MonoBehaviour {
    8.  
    9.     public static Pianist pp;
    10.     Text box ;
    11.     bool play, intoB ,bar;
    12.     string txt;
    13.     int index, size;
    14.     float wait;
    15.     // Use this for initialization
    16.     void Start ()
    17.     {
    18.         if (pp == null)
    19.         {
    20.             pp = gameObject.GetComponent<Pianist>();
    21.         }
    22.         box = gameObject.GetComponent<Text>();
    23.         play = false;
    24.         index = 0;
    25.         wait = 0.0f;
    26.         StartCoroutine(PPiano());
    27.     }
    28.    
    29.  
    30.     IEnumerator PPiano()
    31.     {
    32.         while (true)
    33.         {
    34.             txt = box.text.ToString();
    35.             size = txt.Length;
    36.             if (play && (size > 0) && (index < size))
    37.             {
    38.  
    39.                 if (txt[index] == '[')
    40.                 {
    41.                     intoB = true;
    42.                 }
    43.                 else if (txt[index] == ']')
    44.                 {
    45.                     intoB = false;
    46.                     wait = 0.2f;
    47.                 }
    48.                 else if (txt[index] == '|')
    49.                 {
    50.                     bar = true;
    51.                     wait = 0.5f;
    52.                 }
    53.                 else if (txt[index] == ' ')
    54.                 {
    55.                     if (!bar)
    56.                     {
    57.                         if (intoB)
    58.                         {
    59.                             wait = 0.1f;
    60.                         }
    61.                         else
    62.                         {
    63.                             wait = 0.3f;
    64.                         }
    65.                     }
    66.                 }
    67.                 else if (char.IsLetterOrDigit(txt[index]) || char.IsSymbol(txt[index]))
    68.                 {
    69.                     bar = false;
    70.                     yield return new WaitForSeconds(wait);
    71.                     InputSimulator.SimulateTextEntry(txt[index].ToString());
    72.                     if (intoB)
    73.                     {
    74.                         wait = 0.0f;
    75.                     }
    76.                     else
    77.                     {
    78.                         wait = 0.2f;
    79.                     }
    80.                 }
    81.                 index++;
    82.             }
    83.         }
    84.     }
    85.  
    86.  
    87.     public void SP(bool change)
    88.     {
    89.         play = change;
    90.         return;
    91.     }
    92.  
    93.     public void RP()
    94.     {
    95.         play = false;
    96.         index = 0;
    97.         wait = 0.0f;
    98.     }
    99. }
    100.  
    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 ?
     
  2. luke_2

    luke_2

    Joined:
    Nov 20, 2012
    Posts:
    29
    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.
     
    SirMabrouk likes this.
  3. SirMabrouk

    SirMabrouk

    Joined:
    Jul 28, 2017
    Posts:
    2
    Thank you!! It worked
    Also thank you for explaining