Number Wizard Selectable Range Issue

Hey guys, I’m new to Unity, C# and programming in general - I just started this course a few days ago.

After completing the section 2 of the course I decided to try taking on the challenge presented in the section 2’s Wrap Up - to add a feature to the Number Wizard which would enable the user to pick the range of numbers from which the computer would have to make a guess.
I knew it would be pretty challenging since I have virtually no prior experience of programming, but decided to do it because I had a feeling I could learn a few very useful things by doing this.

After a few hours of searching through the Unity API and reading posts about similar subjects on various forums I came up with an idea how to do it using the “Console.Readline” Method available in C#. However, after writing the code, the part of the code containing the Method was coloured red by MonoDevelop and after searching online a bit longer I found out that “Console” didn’t even exist in C# as an I/O (input/output) class.

So my question is : could anyone tell me if there’s another way of getting keyboard input for such a purpose when using Unity , apart from using the GUI ?

I appreciate a healthy challenge, but if the GUI is the only way to get the user input from the keyboard for the purpose of this script, I think it’s really outside of the scope of everything we learned in section 2.

After a quick search, i found this…
http://answers.unity3d.com/questions/545217/unity-debug-console-i-want-to-input-things-during.html

may not be of any use to you, but definitely worth a look in. May test it myself.

I also used

max = 1000;
min = 1;
guess = Random.Range(min, max);

for random guess range, and works reasonably well.

I shall get back to you regarding that link posted above :slight_smile:

Thx for the reply :).

Yeah I already found out about Random.Range myself while searching online, but what I wanted was to enable the user to set the range in which the computer would have to make a guess, not to randomize the computer’s guess in a range already predetermined in the script.

The link you provided also suggests what I said in the original post - that the only way to do this is by using the GUI.

Oh well, I guess I’ll leave this be for now and get back to it once I’ve actually learned how to set up the GUI.

Thx for the help .

No worries. yeah i very quickly found that out lol.

Good luck in your searching, and if i find anything in the mean time i will hit you up :slight_smile:

In the wrap up video they allude to the fact that Unity doesn’t really have a great way to accept input from the console. Of course, if the only input you care about is numbers, you’re only dealing with a small subset of possible key inputs, so you could hack it with Input.GetKeyDown() if you wanted to be able to read in an arbitrary number.

Here’s an algorithm give you some intuition for the code needed:

  • Initialize a variable to 0, then wait on input from the keyboard
  • If the keyboard input is a digit, shift the current input value to accept a new most significant digit (i.e., multiply by 10), then add the new digit to the previous input
  • Echo the new input value to the console to keep the user informed and happy
  • When the user hits Enter, stop updating the variable and indicate to your program that it should do something else when it detects a key press

This solution is profoundly hacky, and the steps above are still far from a complete implementation. If you do implement it though, you’ll probably gain a new appreciation for how much is actually happening behind the scenes when you do something as apparently simple as type text into a computer.

I read your post and I think I understand how to do it.
I agree with what they said in the video now - it seems pretty “clunky”, but I’ll do it myself anyway, to gain - as you said - an understanding of how this stuff operates behind the scenes. : )

I appreciate the help.

Hey , I have one more questions , if you don’t mind : )

As I said I’m new to C# and programming in general, so while I understand the concept of “statements” in programming, I don’t really know all of them and how to use different kinds of statements.

From what you suggested I thought about using something like this :

void Update ()
{
if (Input.GetKeyDown (KeyCode.Alpha1)) { min = 1; }

else if (InputGetKeyDown (KeyCode.Alpha2)) { min = 2;}
} …and so on.

However the if/else statement only allows the function to receive the input from one of the statements, so if I want to piece together multiple pieces of input I think I need to use some different kind of statement or loop.

I know about the Int.Parse Method but that’s can’t help me assign the input to a variable if I can’t use Console.Readline, which Unity doesn’t support.

So, do you know what kind of a loop or statement I could use when I get need to keep track of multiple inputs via InputGetKeyDown ?

I’m a Unity noob, so I don’t necessarily know all the intricacies, but I would do something like:

//set up some flags to determine which "mode" you're in
//boolean variables are only allowed to contain values of true or false
bool settingMin = true;
bool settingMax = false;
bool playingGame = false;
int min = 0;
int max = 0;

void Update() {
    if (settingMin) {
        //put your code to read the first input in here
        if (Input.GetKeyDown(KeyCode.Alpha1)) {
            min = min * 10; //shift the current value
            min = min + 1;  //increment the current value based on the keypress
            print(min);     //echo the current input value to the console
        }

        //repeat for each possible key press

        if (Input.GetKeyDown(KeyCode.Return)) {
            settingMin = false;    //the next time Update() is called, none of the code in the outer if statement will execute
            settingMax = true;     //instead, code in the next if statement will execute
            print ("Minimum value is " + min);    //it's good form to let the user know you heard what they said
        }
    }

    if (settingMax) {
        //put similar code here to set the max value
        //set settingMax to false and playingGame to true after input is finished
    }

    if (playingGame) {
        //put all your gameplay code in here
    }
 }

Wow I never even thought about using bool. I saw other people’s scripts online containing bool variables but I didn’t understand what they needed bools for until now.

I understand what they said in the video about using “StateMachines” now - you actually use a boolean variable to achieve this. :slight_smile: Thx !

That answers the part about the min and max setting and the state machine , but I still have the problem of not being able to put together multiple inputs into one “min” or “max” variable.

that’s what I tried to describe earlier - if I’m using an if statement like

if (Input.GetKeyDown(KeyCode.Alpha1)) {
min = min * 10; //shift the current value
min = min + 1; //increment the current value based on the keypress
print(min); //echo the current input value to the console
}

I can assign the value of 1 to the variable min, but if I ,for example, want to assign the value of 12 to min, I don’t know how you can do that by using an if statement ?
If I used

if (Input.GetKeyDown(KeyCode.Alpha1)) {
min = min * 10; //shift the current value
min = min + 1; //increment the current value based on the keypress
print(min); //echo the current input value to the console
}
else if (Input.GetKeyDown(KeyCode.Alpha2)) {
min = min *10;
min = min + 2;
print(min);
}

I could only assign either 1 or 2 to min . If you’re using the if / else if statement, there’s no way to keep/memorize the value you assigned to the variable in the first if statement. It’s just a choice between 1 or 2. So after you’ve picked let’s say 1 , if you press 2 and activate the else if statement, it doesn’t add to the value you assigned to the variable by pressing 1, it just overrides it .

If you’re new to Unity too maybe it isn’t that easy for you to come up with a solution for this either - so thx for the help in any case, I still don’t know how to solve this completely but I did learn a few things.

edit : I think I actually might have figured it out - you don’t use else if on the second statement , just another regular if. sorry for the bother : )

The line min = min * 10 takes care of that. Think about what it means to multiply a number by 10 and how that relates to the problem of “building” a number from individual digits. It shouldn’t matter whether you use if or else if unless you want to be extra sure a user can’t press multiple keys at once.

I understand the part about multiplying it by 10 - it’s just so that the first digit would move one space to the left so that you can pick digits directly.
Without multiplying it you would just be incrementing it regularly , so if you pressed 1 followed by 2 you wouldn’t get 12 but 3 instead.

Yeah I guess you’re right about if and else if, it doesn’t actually metter. I was just a bit confused due to my inexperience with using the statement, and I thought else if would override a previously selected if statement :slight_smile:

Here is what I used to help solve the user input problem.

https://docs.unity3d.com/ScriptReference/Input-inputString.html

You don’t need to use GUIText. I just used a class string variable to store the input until enter was pressed at which point you can set/save the value and move on. Remember though if doing that you need to do special conversions to change the string variable to integer.

Feel free to have a look at my code if you like:
http://pastebin.com/RNVY9KcB

1 Like

HOLY (insert rude word) !! you are my hero Mr. Jordan :joy: i have been searching for a way to do just that for hours before i decided to take a look on the udemy forums hahaha thank you my man! and yeah i know this is a few months late :frowning:

1 Like

Hey @Josh_Ray,

thank you for giving insight in your example.
I’m impressed by how clean and sophisticated your code is.

If you were a ****** beginner like me when you did this, I really feel ashamed now, that I didn’t push myself hard enough to reach the same level.

1 Like

Hey, no problem and happy to help. I’m actually not a beginner. I already
have a programming degree and some years of experience doing web and
desktop apps as well as a bit of previous Unity/C# experience so no need to
feel ashamed. We all started somewhere. Best of luck

2 Likes