Unity UI Tutorial: Set Text On Button Click C# Event

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is a short unity tutorial about setting the text of a UI text field when a button is clicked so we start here in unity 2019 with a simple almost empty scene we just have a camera and a light and the first thing I do is to add the UI components the button I just right click in the hierarchy and choose UI button then it is created and also automatically a canvas and an event system component then I enabled the 2d view and to set the button to the center of the canvas I set the X and the y position to zero okay then I press the F key to bring the button interview and the next component that I add is a text field I select the canvas in the hierarchy and choose UI text ascent of the text vertically and horizontally increase the font size a bit and then move it upwards above the button all right that's the whole UI we need and now let's go ahead and program the c-sharp script I create a folder and name it scripts and to this folder I add a c-sharp script and name it button text then I assign the script to the button I just drag it on to the button game object and then I double click it to open it in Visual Studio 2019 which is my code editor okay this is the class button text and we don't need the start and the update method so I remove it then we need a public reference to the text field so that we can assign it in the unity editor the class text is not known at the moment we have to include the appropriate namespace and the easiest way is to show potential fixes and then select using unity engine UI I set the name of the variable to text field and the next thing we need is a public method to set the text this method a name set text and it has one parameter of type string that contains the text that I want to set and the implementation is very simple I just assign this text to the text member of the text field then we can remove the usings that we don't need and this is the whole c-sharp script very simple right okay back in the editor I assign the text to the text field slot of the button text script and the button has an on click event that you can bind for any press this plus icon what I want to do is to call the method set text that we just implemented when this onclick event is invoked so what we have to do is to drag the button into this slot of the on click event because it has the script component assigned that we want to execute and here you can see it button text with the method set text and it has one parameter as you know and this can be configured here in this text field as a parameter value I choose my text all right and now we can start the game and when I press the button the value of my text is assigned to the text of the text field okay as I said a very short one but I hope the request is answered with this tutorial but if you still have any questions at these to the comments don't forget to subscribe if you haven't already stay tuned follow me on my Instagram Twitter Facebook and it would be great if you'd like to support me as my patron thanks a lot and I see you on China
Info
Channel: Jayanam
Views: 43,126
Rating: undefined out of 5
Keywords: unity change text with button, unity c# set text, unity text ui, unity text ui script, unity text script, unity button onclick, unity button click event, unity3d, unity, unity button, unity text input, unity c# button click event, unity c# button script, unity button ui, unity ui tutorial, unity ui c#, unity ui text change script, unity ui text variable, jayanam, jayanam unity
Id: xpcyZdyO5P8
Channel Id: undefined
Length: 4min 17sec (257 seconds)
Published: Thu Apr 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.