HOW TO DISPLAY TEXT FROM AN INPUT FIELD USING C# UNITY TUTORIAL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys Jimmy Baker see it and in this mini unity tutorial I'm going to show you how you can display text from an input field in just a normal text box don't forget click the subscribe button and click the bell icon as well to stay up to date with every tutorial I upload to this channel and with that in mind let's get to work so let's say your game has a little input field where you can say put your name in and then that name is put into text boxes you know wherever else in the game it's a lot easier than what you would think um I think a lot of people get confused with the input field and how to do it via c-sharp so let's get to it so if we go to game object and let's go to UI and you'll see down here we have something called input field and I'll put it inside your canvas with a couple of different options what we're going to deal with mainly is text this is one where we get to actually have whatever we enter say our name or whatever next one we do is I'm going to add in so I'm just normal text that we can bring in so if I double click this I'm going to remove the post processing so we can see a bit better in our scene let's just quickly have it why because you guys already know with what your text looks like so I'm just kind of putting a temporary one in here just so we can see it working so I wanna have that text say nothing right now but have it centered and I'm obviously gonna have a button in there as well because I'll explain why you don't want to do it one way we're at constant updates I'll explain that when we get to the coding so if we have a button and that button it just says no enter so that button will be we press it when we have our name so the way we do this is make sure you do have that input field and just make sure you know where to reference your text when you actually build script so in your asset window right click create and c-sharp script will call this name transfer and open up in a visual studio so there's a couple of things we need to do in this script it's not complicated it's just a couple of lines of code so obviously we need to create some code for the button as well so that'll be its own method inside the script a couple of variables obviously one variable would be the textbox the other variable would be the text that we're taking it to and we'll also have a string as a variable as well so we can store any information in that string whether it be for this scene or whether it be for any other scene but I say I'll explain that as and when we get there quick note about the scene that I'm in at the moment it's just a quick little scene from the asset store probably gonne desert I think it's called check it out it is quite a nice scene okay so we're in c-sharp visual studio to write some C sharp at the very top we'll need to put in a name space which is using unity engine GUI semicolon because we'll be dealing with UI elements and what we'll need to do is we'll need to declare a couple of variables and let's get rid of void update and void start they can disappear so the first variable so public and this will be a string s is in a lower case because it's just where it's defined here let's call it the name so this is the name you're entering the next one is gonna be public game objects and this one will be the if that we'll call it input field so this will be where we're typing our name in this case and the final one will be the text so whatever you have is your output where you want to display whatever you're putting in the input field this will be that game object so game object and we'll put text display semicolon so the idea of what we're doing here is when we press the button to say and this is what we're gonna do with it so it's gonna have to be a public void because it is a button and we'll say store name I guess it doesn't really matter too much what we call you can call this method anything at all so what we need to do is have the name equals and it is input field fancy but it right input field get component and spray key brackets text over closed bracket dot text semicolon and then to display it we have text display dot get component and spray key brackets text dot text equals welcome or whatever your text would be then plus the name plus to the game or whatever your text would say semicolon so the idea of what we're doing here we're taking whatever our text box should say and we're closing out the first little bit cause they have the second little bit and in between we're putting plus and then this string and then plus the rest of the text so if I save that script now and then head back into unity that's at my desk there for some reason let it compile and there we go so we need to attach this to a game object so I'm just going to create a quick random empty game object and just attach it to there then we just need to define a couple of variables so the input field itself is not the main input field it's actually the text object which is within the input field so just make sure you get that bit right that bit is I think what confuses most people so obviously text display is wherever you want your name to appear after that what we need to do is solve the but now it's on the button click on + bring a game object over if you don't know how to use a button I do have other tutorials on it I'm just kind of going through this pretty quick so I'm just setting my button to run that method of store name so all it really is is just a way of the button says yep do what you need to so if we press play now and go to our scene everything's so good so far and I'm gonna put Jimmy in there enter now we go welcome Jimmy to the game can't quite see that so well so I'm just gonna bring it maybe across a little bit so we can see there we go welcome Jimmy to the game so for change that to Bob there we go so this input field you can put anything in and because the way we've got the script set up it will take it and place it wherever you want it to be so that is how we can use an input field to enter a name a place anything you want and then display it in text right there that is the line of code you need more than anything to do it so guys I hope that's helped and yeah thank very much for watching
Info
Channel: Jimmy Vegas
Views: 106,721
Rating: undefined out of 5
Keywords: unity tutorial, unity tutorials, how to make a game, brackeys, jimmy vegas, unity3d, game development, c#, programming, coding, video game, first, noob, assets, beginner, easy, how, to, learn, tutorial, tutorials, tip, game, development, develop, games, make a game, for free, survival horror, unity 3d, fps, gta, grand theft auto, unreal engine, indie, beginners, asset, model, texture, models, textures, howto, course, series, basic, basics, how to make, getting started, level design, introduction, intro
Id: 2liZtyMhIQQ
Channel Id: undefined
Length: 7min 44sec (464 seconds)
Published: Wed Apr 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.