Input function works, but still.. how exactly?

Hi all,

I am having troubles understanding the user input part. Below is my code, which is basically copy-paste from the tutorial video, with a few slight modifications and it works just fine. Nonetheless, being quite new to programming with C# and having only practiced on console programs up until now, I am used to a different type of procedure to retrieve an user input. I would be using the following command:

string userInput = Console.ReadLine();

The variable userInput, I could then use in an if loop and trigger an adequate reaction.
In our tutorial code however, I do not understand exactly how the input “retrieval” is taking place. We define a method called OnUserInput and a string type variable called input. But to me it is not really clear how the actual input from the user is saved into our variable input. For all I know, we could call the variable cucumber. Is there any hidden method / function running in the background, or what else am I missing here? Could you help me out please? Much appreciated!

Looking forward to hearing from you!
Best Regards,
Ilmay

Just have gotten to the Instructor Hangout 2.1 and heard that there is actually a method / function running in the background. So I’m going to leave this topic for now, until I get to the point where I have the adequate know-how to interpret the actual process. If anyone chooses to answer beforehand anyway, still much appreciated! :slight_smile:

Hi Ilmay,

Unity is not a C# console application. For this reason, Console.ReadLine() does not work. In fact, processing user input, which is longer than one character, is relatively difficult/complex because you have to manually do that with your own class/instance.

Fortunately, Ben and Rick provided scripts which do the job for you. See the WM2000 folder. In one of the scripts, you’ll find “OnUserInput”. And that’s why OnUserInput in your code works.

The Unity 3D course caters to beginners. You will not learn everything you need to grasp everything what’s going on in Ben’s and Rick’s “secret” classes.

However, you could dive a bit deeper into C# with Bob Tabor’s C# course, and when you completed it, you could do some research by yourself in the C# API and elsewhere on the internet to deepen your knowledge.

Hopefully, this helped a bit. :slight_smile:

Hi Nina,

thanks a lot for clarifying. Figured it was something I missed :slight_smile: Anyway, not going to try to run before I have learned to walk properly. Your advise is of course very helpful and much appreciated!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms