UI Button - Unity Official tutorials

Unity · 218K views · 2014 · 13:13

Open on YouTube

Game programming & enginesIndie game developmenttutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering Unity questions about UI Button OnClick events, such as methods not appearing in the inspector or how to wire up a click handler.

Recommended by 3 people across 3 different months, 2015 to 2018.

What it is

An official Unity tutorial from 2014 that explains what a UI Button is made of and how it works in Unity's UI system. It walks through the Button component, its transition states and how to hook up OnClick events from the inspector.

What people said

Recommends Jacksonkr · 2015-02 · Stack Overflow · on “GUI of Unity 4.6 - Detecting On Click() Down”

I saw this youtube video after viewing your question. This video is the CORRECT way to do what you're wanting to do. The video is only 10 minutes so I suggest you watch the whole thing. [this video]

Recommends Gunnar B. · 2016-05 · Stack Overflow · on “Unity UI Button not showing my method”

Actually I think I see your mistake. From the screenshot it looks like you dragged the script directly from the assets folder into that spot for the OnClick , but you need to drag the gameobject that has that script on it from the scene into that spot. This shows how it should be done (the object dragged has to have the script): [this video]

Recommends Hellium · 2018-05 · Stack Overflow · on “Unity is not showing public methods in the event field”

`CreateInventory` has **two** parameters, and Unity is not able to handle functions that are 2 or more parameters. Check the Unity tutorial here [this video]

Cites Martin Dawson · 2016-04 · Stack Overflow · on “Unity 3D: Changing sprite animation using UI button”

Buttons have an OnClick event [LINK] You just create a method that gets called when the button is clicked, in your case the changing sprite code. Seen as you are using a timer though you will need to use something like a bool because onClick() only gets called once when clicked, not every frame. Look [this video] bool b_RunSpriteAnim; public void onClick(){ b_RunSpriteAnim = true; } void Update(){ if (b_RunSpriteAnim) //your anim sprite stuff } Then once the sprite anim has finished, just toggle b_RunSpriteAnim to false and reset the timer. Edited: You don't need a boolean. I only thought you wanted it because you were using a timer (as based on the Youtube link). If you just want to change the s

Cites Scott Chamberlain · 2017-04 · Stack Overflow · on “C#/UNITY How do I make a button change the screen resolution?”

What specific part are you having trouble with? Getting the button to call the function `Resolution11` or getting the screen resolution to change after calling `Screen.SetResolution`, if it is just getting the button to work take out all the extra parts of your question having to deal with screen resolution and focus your question on the problem you are actually having. Once you change your question to "How do i make a button call a function in unity" I am sure you will find turorials explaining how to do it. Example: [this video]

Cites Hellium · 2018-05 · Stack Overflow · on “Unity is not showing public methods in the event field”

As indicated in the official Unity tutorials [this video] , if you want to provide a function to an event in the inspector, the function must meet the following requirements: The function must be public The function must have a return type of void The function must take no or one parameter If the function takes one parameter, the latter must be one of the following types: int float string bool UnityEngine.Object , or any type inheriting from UnityEngine.Object (such as GameObject , MonoBehaviour , ScriptableObject , ...)