Unity Button Click Events C# Script Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys here is a new unity video for beginners in which I'll show you how to add a UI button to a canvas and then react in a c-sharp script on clicking the button I start with an empty unity project this is unity 2018 and the first thing I add is a UI canvas the canvas is a kind of transparent plane over your screen in 2d to which you can add your I components and I will add a button therefore I'll right-click the canvas and choose UI button you can move the button around on your canvas you can also anchor it for example here to the top of the canvas but this is not a tutorial about layouting your eye I will go more into detail in one of the next videos ok now we have that button and the button consists of the button itself and a child component of type text the text can be changed here in the inspector for example let's use my text what you can also do to make this button look a bit more interesting is to change the colors there is a normal color this is the default color then there is a highlight oppressed and a disabled color you can see the effect of my modifications when I start the game and then for example press the button but what I want to do in this tutorial is to change the text of the button dynamically by using a c-sharp script to do this first add a new c-sharp script of course and our call this button handler ok then double click the script and it will be opened in visual studio if wishes studio is your current editor ok here we go we don't need the start and update method here so let's remove it but add a new method called set text it has one parameter of type string and for this we will pass the text that we're going to set for the button so we want to set the text for the UI text component so we have to include or to use the namespace unity engine dot UI and then we have to find the child component called text of the button because the button as I mentioned contains an object called text that has a text component of type text so what we have to do is to get this component of type text then we could for example assign it to a local variable and then finally assign the text parameter to the text property of this component okay that's it now you can select the button and then add an on click event and he assigned the button that has the button handler attached and for the button handler you find the method set text then you can use this text box here to define the parameter of the set text function and set this year to new text now when I start and click the button the text is set to new text if you like you can go ahead and copy this button by pressing ctrl + D and for the parameter we could use a different text let's be creative new text tool okay guys that's it for the button click tutorial for beginners to unity I really hope you like it and you'll find it useful and if you do don't forget to subscribe if you want to support this channel and help it grow please think about being my page when this would really help a lot thanks for watching thanks for your support and see you in the next one
Info
Channel: Jayanam
Views: 333,365
Rating: undefined out of 5
Keywords: Unity button click, unity button c# script, unity on click event, unity button click event, unity C# script for button, unity OnClick C# Script, Unity UI BUtton, Unity Canvas and BUtton, unity beginner tutorial C#, Unity beginner tutorial button, button, unity, unity 2018.1, unity 2018, unity ui buttons, unity ui programming
Id: kdkrjCF0KCo
Channel Id: undefined
Length: 4min 10sec (250 seconds)
Published: Sat May 19 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.