VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/buttons-and-events-in-unity/

⇱ Buttons and Events In Unity - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Buttons and Events In Unity

Last Updated : 4 May, 2026

A game without buttons is like a TV without a remote i.e the player just watches, unable to interact. Buttons let players start games, pause action, buy items, or quit. This article shows how to create buttons and make them work.

Button

A Button is a clickable UI element that triggers a function when pressed. Unity creates it with a background image and text label.

👁 Button-In-unity
Button In Unity

Creating a Button

  • Right-click in Hierarchy - UI - Button - TextMeshPro
  • Unity automatically creates Canvas, EventSystem, and the Button.
👁 Create-Button-In-Unity
Create Button In Unity

Connecting Button to Function

Step 1: Create a script with public methods

Step 2: Connect in Inspector

  • Select Button → Find OnClick() section → Click "+"
  • Drag GameObject (with script) into slot
  • Select your function from dropdown

Output:

👁 Connect-Button-with-Function-In-Unity
Connect Button with Function In Unity

Pause Menu Example

When the game is paused, time stops and a menu appears. This script handles pausing and resuming.

Time.timeScale = 0 freezes all movement, physics, and timers. Set back to 1 to resume normal gameplay. The pausePanel is a UI Panel that appears when paused.

Enable/Disable Button

Sometimes a button should not be clickable – like a "Buy" button when the player has no coins.

  • interactable = false: disables the button, It becomes gray and ignores all clicks. 
  • interactable = true: re-enables it.

Multiple Actions on One Button

Click the "+" button multiple times in OnClick section. Each action runs in order when button is pressed.

👁 Multiple-Actions-on-One-Button-In-Unity
Multiple Actions on One Button In Unity

Button Visual States

Button automatically changes appearance based on interaction:

  • Normal: Default look
  • Highlighted: Mouse hover
  • Pressed: Clicking down
  • Disabled: Not interactable (gray)

You can change colors for each state in the Button component under "Transition".

Comment
Article Tags:
Article Tags:

Explore