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

Unity freeze when running a very basic script

Discussion in 'Scripting' started by PandaSekh, Mar 3, 2020.

  1. PandaSekh

    PandaSekh

    Joined:
    Feb 24, 2017
    Posts:
    2
    Hello,
    I just started learning Unity/C# but I'm having a problem with one of my first scripts. When I try to run this script it Unity freeze and I have to close it using the Task Manager.
    This is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class NumberWizard : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         int min = 1, max = 100;
    12.         Debug.Log("Hello and welcome to Number Wizard");
    13.         Debug.Log("Please choose a number between 1 and 100");
    14.         Debug.Log("Let me guess your number...");
    15.         GameWizard(min, max);
    16.     }
    17.  
    18.     static void GameWizard(int min, int max)
    19.     {
    20.         Debug.Log("Your number is " + GuessNumber(min, max));
    21.         Debug.Log("Is it correct? Tell me if it is (C)orrect, too (B)ig or too (S)mall");
    22.         string userInput = Console.ReadLine().ToLower();
    23.         switch (userInput)
    24.         {
    25.             case "c":
    26.                 Debug.Log("I was right! Nice :)");
    27.                 return;
    28.             case "b":
    29.                 Debug.Log("Retrying...");
    30.                 GameWizard(min, GuessNumber(min, max));
    31.                 return;
    32.             case "s":
    33.                 Debug.Log("Retrying...");
    34.                 GameWizard(GuessNumber(min, max), max);
    35.                 return;
    36.         }
    37.     }
    38.  
    39.     static int GuessNumber(int min, int max) => (min + max) / 2;
    40.  
    41.     // Update is called once per frame
    42.     void Update()
    43.     {
    44.        
    45.     }
    46. }
    I tried to use this code for a Console App and it works as expected, I can't figure out what the problem is.

    Thank you in advance.
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    Oh neat. I learned something from this question. I had not thought to call into the System.Console from within unity. You can't do that.

    The short answer is that you can't use the System.Console.ReadLine() method. You should use one of Unity's ways of getting user input. Unity's static Input class is probably the easiest to use right now.

    As the simplest way, I suggest putting your switch case into the Update method:
    Code (csharp):
    1.  
    2. if (Input.GetKeyDown(KeyCode.C)) {
    3.     Debug.Log("I was right! Nice :)");
    4.     return;
    5. }
    6. else if (Input.GetKeyDown(KeyCode.B)) {
    7.    // do the "b" case here
    8. }
    9. else if (Input.GetKeyDown(KeyCode.S)) {
    10.     // do "s" case
    11. }
    and so on
     
    Joe-Censored likes this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    if user input is not equal to c you'll be stuck in a recusive loop, I'm pretty sure new input won't be recived unless you use a coroutine and wait a frame..

    also, how does console.readline work in unity..?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    It doesn't.
     
    Joe-Censored and SparrowGS like this.
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    thought so, lol.
     
  6. PandaSekh

    PandaSekh

    Joined:
    Feb 24, 2017
    Posts:
    2
    Thank you
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Now I'm curious about trying Console.ReadLine() from a headless build lol

    But to the OP, you probably want to use an inputfield and a button to say when done. It is also possible to capture the enter key in place of a submit button, but that is more complicated than you'd think (though there are threads with code examples on doing so).