Main

Unity button not working? Here is the right way to setup with script.

This tutorial will teach you how to create a simple Unity UI button and call a function when the Unity button click is detected. Get code samples on https://vionixstudio.com/2022/05/21/making-a-simple-button-in-unity/ #unity3d #unity3dtutorial

VionixStudio

1 year ago

Welcome to VionixStudio. Let's start making games.  Hi and welcome. In this tutorial, we are going to see how to add a simple button in unity. And then  we'll see how to call a function when the button is pressed. To add a button just click on the  plus sign in the hierarchy go to ui and button if you double click on any  element on the hierarchy it will zoom to that element in the scene view now move the button to the  place where you want it so i'm going to move it to the center of the screen
and as you can see I have a button. So let's see  how it looks in the game view. So this is how it looks in the game view. The next thing we'll see  what the button has. The button also has a text component to it. If you're using older version of  unity you will have a normal text but in the new version of unity you only have text mesh pro  we'll see about text mesh pro a little later in the button game object there is an  image component and a button component the image component determines how
the button  looks in your scene. So for example you can select any other sprite as a button. So if you select  the knob as a button the button looks like this so you can also have custom game objects  for example i have a character here to use this character as a button sprite it  should be set to sprite 2d and ui and click apply then go to the button so you can even use this  character as the button image. This is is my button now. So now we'll see how to call  a function when the button is pr
essed. So if you select the button game  object and go to the button component you can see there is an on click event. Now when you click plus  sign here you can see this on click event takes a game object and the function attached to the game  object as input so generally for all the ui things we have an empty game object which is called scene  manager or you you can name it however you want it so in the scene manager let's  add a script called button press inside the button press script you ne
ed to have a public function this function  button pressed is a public function and when this function is called a log is displayed on the  console saying button pressed. So now let's go back to the button game object go to the  on click event and drag and drop the scene manager to your on click game object. Now you  should see the button press script and the button pressed function. So  select the button pressed function and now if you play the game and go to the console window and press  the
button the message is displayed that's it you've created a simple button.  Now you know how to call a function when the button is pressed. Thanks for watching  don't forget to like, subscribe and share.

Comments

@byond9315

Thank you so much, I was trying to drag the script in all this time, didnt realize it was a gameobject.