Welcome back.
In this video, we are going to have a look at buttons.
And buttons are very important because they allow us, for example, to move from one scene to another.
They allow us to choose at which level we want to be there.
Important to change settings, for example, or within your settings menu in general to play around.
So buttons are used very commonly.
So it's important to understand how they work.
And in order to add a button to our UI, we need to create a button within our canvas.
So right click onto canvas, then go to UI and choose button.
What that will do, it will add a button and that button has a child, which is our text.
So the button itself has a text property or a text child, which you can then change any time.
This text is similar to the text that we have seen here.
The only difference is that it has a that it currently stretches to the size of its parent.
So its anchor settings and its anchor preset is said to stretch towards all directions as much space
as you have, which means this text will take up all the space that the button gives it.
All right.
Button itself has properties as we know them.
For example, the transform we have worked with that.
Then it has a width, a height and so forth.
It has an image script which has a property or a variable called source image, which you can change
any time.
So if you want to change the appearance of your button, you could drag an image into that source image
property.
All right.
So now let's get to how we can make this button active interact with.
Because currently, if we activate the game or start the game and we press on the button, as you can
see, it indicates that something is pressed or that we press it, but nothing happens.
And in order to make something happen, we need to have a script which has a functionality that we want
to call and there are multiple different options.
You could add a new empty object where you assign that script to, and then you can assign the script
to the button on Click Event.
Or you could do the same thing with the button itself.
So we can add a component here, which is a new script, and I'm going to call that button behavior.
And what that script should do is for now, it should increase the score by one.
So let's go into button behavior.
So what we can do is, for example, we can try to access the text behavior script from our button behavior
script, and then we could, for example, increase the text number and then set the text number in
order to change the value.
We can do that within button behavior itself as well.
We could get access to the text UI element in here and then access it from here.
Let's use this approach.
So let's create a public text and in order to use text, of course, we need to have unity engine UI.
So the namespace unity engine UI.
Now we can use the text class, let's call it score text.
And every time that we press the button, we want to increase the score text by one.
So let's get rid of the start and update method and create our own method, which I'm going to call
public void on button pressed.
So let's say when I press the button, I want to change the score, text the text to I or button was
pressed.
All right.
So let's try that.
Let's add that to that script, to our button or it is already saved.
But now we need to wait a little bit until text will be shown.
So the text property here, as you can see, score text is there.
And I'm going to use the text as our score text, which is this text here.
It's important to use the text on top of button and not within button.
All right.
So now I need to add an on click event.
So here you can see there is an on click list list is empty currently and we can press the plus button
and then we need to give it an object which contains the script that has the method that should be triggered
whenever we click on that button.
All right, so click on button or choose button here because button contains the button behavior script.
If you would have the script in a different game object, then you would have to choose that different
game object as the one, the selected object that you select here.
So I select button and now I get this button behavior added to my options.
Before that it wasn't available and now I can choose on.
Button pressed.
So let's do that.
On Button Press was chosen.
Now, let's go ahead and run the game and see if our button does what it should.
So let's press the button and we see t0n was pressed so our text is changed to button was pressed.
If I press SW you see s was pressed.
If I press if I press the space key.
It says button was pressed as well.
All right.
So let's press the button.
And as you can see, it changes the text value.
So that's how simple you can use button functionality.
And yeah, that's it.
So now you know how to use buttons.
In the next video, we will have a look at how we can use buttons in order to get from one scene to
another.
So see you there.