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. Dismiss Notice

While(true) code breaks program

Discussion in '2D' started by wcburg1, Mar 28, 2018.

  1. wcburg1

    wcburg1

    Joined:
    Mar 23, 2018
    Posts:
    19
    I am trying to use an infinite loop (while(true)) to wait for a user to click the spacebar before executing the meat of a certain code. The context is that all this occurs in OnMouseDown(), meaning when the user clicks on a certain gameobject, it executes the infinite loop, then breaks when the user clicks the spacebar and continues on to the meat of the code. This loop is causing my program to freeze. It won't let me even quit the scene or do anything in it; my only choice is to use task manager to kill the unity editor altogether. Can anyone tell me why this is happening? Here is the whole function:
    Code (CSharp):
    1. void OnMouseDown(){
    2.         //When mouse clicks, waits for a keyboard input from the user, then executes the meat of the code.
    3.         transform.localScale = new Vector3 (20f, 20f, 0f);
    4.  
    5.         while (true) {
    6.             if (Input.GetKeyDown(KeyCode.Space)) {
    7.                 break;
    8.             }
    9.         }
    10.             //Meat of the code.
    11.             input = "H";
    12.             string x = find (input, I1);
    13.             getValueText.text = x;
    14.             getValue.SetActive (true);
    15.             transform.localScale = new Vector3 (10f, 10f, 0f);
    16.                
    17.        
    18.    
    19.     }
    Thanks much!
     
  2. marcV2g

    marcV2g

    Joined:
    Jan 11, 2016
    Posts:
    115
    You don't want infinite loops since now 100% of your computer resources are just going in circles

    try putting

    if (Input.GetKeyDown(KeyCode.Space)) {

    in a Update() method

    EDIT:

    Code (CSharp):
    1. private void Update()
    2. {
    3.             if (Input.GetKeyDown(KeyCode.Space))
    4.            {
    5.            
    6.            //Meat of the code.
    7.             input = "H";
    8.             string x = find (input, I1);
    9.             getValueText.text = x;
    10.             getValue.SetActive (true);
    11.             transform.localScale = new Vector3 (10f, 10f, 0f);
    12.             }
    13.  }
    14.        
    15.  
    16.    
     
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Yes, what @marcV2g stated. As far as I know, you'll never want (or need) to use infinite loops in Unity. All of the Update functions are essentially infinite loops anyway, so it's better to use them instead.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just to add: If it has occur after the mouse down event, you can add a bool to say it's "ready", and check for that bool && the space key.