Guess Game Unity Tutorial - 2 - Getting Input From The User - Guess The Number Game Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the previous video we have laid out all of our components and now we are going to see how can we get the input from our user for that first I'm going to create an empty game objects will go under game object and click on create empty and I'm going to name this game object game controller and now in our scripts folder create a c-sharp script so right click create and then click on c-sharp script and name it game controller also and attach that script on our game object and I'll simply double click it so monodevelop will open it here so this is that script here we are going to write the code that's going to get the input from our user first I'm going to delete all of these well start and update function and now I'm going to create a public void function and this is going to be get input now what we need to do here is we need to attach a game object on our input field so here if we click on the input field go here in the inspector panel and scroll down we will have this on value change and this end as edit excuse me and both of these take a parameter string which we see here in these parentheses now in order to add a function as a listener to our input field that function needs to be a public void function and also here we need to type the string which is the guest so here I will name it guest parameter because we need to well specify here a string as we see now in order to add this function we can click on this plus button here and now we need to drag a game object and that's why I've created this empty game object so here we need to drag game object and here on this drop-down list we can select all the scripts that are attached on our game objects so here we have our game controller script and now we can select our function so here we have our get input function now we are not going to select it here where I'm pointing with my mouse we need this dynamic string so in order for this to work make sure that you go here under next ring and here select this get input function so now if I go back in my script I can simply debug that log and we can see or print in the console what the user will enter and here we can simply say you entered and we can say plus guess in order well to get this a parameter which is what the user will type in our input field so if I run the game now and now we have our input field and if I type something so let's say this is a guess if I hit enter we are going to see here in the console printed out you entered this is a guess which we will have programmed here so we are using debug that log and this is how can we actually print the console and debug our application we have any problems so in this video I just wanted to show you how can we use this all function in order to attach it on our game object and put in the console what the user writes so one other thing that I'm going to do here is we see here that when we type something so in our case this is a guess this stays right here so let's say this is another guess and I hit enter so this right here stays let's say that we want to remove this text when we hit enter so the user does not need to well delete this text and after that enter another text how can we do that well for that we need to create here a while input field but before that in order to use the UI components we need to type here under using system that collections we need to type using Unity engine that UI and there are a couple of ways to get components so here I'm going to type private input field and let's say input I'm going to name this variable input here in my void awake function and this is a function of monobehaviour and this is the first function that's going to be called when we run the game and here usually we set up initial values now in order to get this input what we can do is we can say input input is equal to game object that find and we specify the name of our game object that we want to find in our scene if we go here in our well program we see that we have this input field which is the name of our game object so we can well copy the name go back here and here I can type this as a string input field and this will find me that game object in the scene and now what I can do here I can simply say input so input that text is equal to an empty string when we get the text from our user so now I can go back in unity and let me just see here what we have and also one thing that I well made a mistake is that we need to type get component here and here we need to pass our input field because we are getting the game object so this right here the whole input field is a game object but we need this input field script and for that I need to type get component and specify excuse me this input field so excuse me for this well mistake and now if I go back in my unity and if I run the game and let's say this is a guess if I hit enter we will see this printed out in the console but the text will be removed from our input field so we see that we don't have the text anymore and we can type here this is another guest and hit again and we see that that is printed out in the console but we don't have it in our input field anymore and one other way how can we also well attach our input field and get a reference to it is instead of using this right here so I'm just going to comment this line out and by the way this is a comment this will not become child so just type these double backslashes and this is how can you document your code so here I'm going to type serialized field above this private input field and now I can go back in my unity editor and select my game controller and here we have this input which is now here in the inspector and I can simply drag and drop this input field right here and this will give me a reference to it so if I run the game now and here I type this is a guess if I hit enter this will be printed in the console and we will not see this in our input field so we see that this is printed in the console and our text is gone so this is another way how can we get or attach our components from our hierarchy panel to our script and use them in our code
Info
Channel: Awesome Tuts - Anyone Can Learn To Make Games
Views: 61,774
Rating: undefined out of 5
Keywords: Awesome Tuts, getting input unity, get input unity3d, input game unity2d, awesome tuts, guess the number game unity, input game unity, getting input from user unity, guess game unity, guess game unity 3d, unity guess number game, how to create a guess game unity, unity how to create a guess game, unity guess game tutorial, get user input unity, unity user input game, how to create a guess game in unity, simple guess game unity, 2d guess game unity
Id: tMgC2yH4Kkk
Channel Id: undefined
Length: 7min 11sec (431 seconds)
Published: Mon Aug 24 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.