Add Functionality to Button or Call Function with Button in Unity 5

With all of the changes to how Unity operates with even basics like Buttons, its easy to read deprecated information that just won’t work in your new Unity 5 (5.5 or 5.6+) projects. It really seems like the major wave of changes started occurring around 4.7, and what happened to how we used to make “buttons” with OnClick() functions or whatever.

However, even now, I still see a lot of misinformation that does actually work, but its the wrong way to do it, and can only lead to confusion or problematic behavior down the road. It is surprisingly easy to do button functionality, and I’ll try to explain each part as I go so you understand the mechanics behind what’s going on.

So we’ll roll with a simple and easy example, doing a step-by-step on how to make make a proper quit button.

1. We’ll be creating a script to attach the button.

2. Then we’ll be adding a call to a function in this script, using the built-in OnClick() list in the inspector.

In prior versions, there really weren’t “buttons” in the Unity engine, so we’d create a GameObject, which we originally had to make our own button scripts for. All of that is no longer necessary. When you create a button, it assigns to it a built-in Button (Script). It has all the basic features you could want, such as changing the highlight/disabled/pressed/etc colors of the button, including transition, fade and “navigation” functionality. It also provides a list which gets called when an actual Click happens to the button.

The important thing to remember is that functionality of other GameObjects can be called inside this list. Usually for simple buttons, you’ll only be attaching a single script (as we will be doing in this example) to the button GameObject, and calling “itself” as the only list item. However, you could conceivably do any number of different actions to any number of objects.


Creating a Script

ButtonClickQuit

I like to name my specific/simple button behaviors with the same preface ButtonClick to maintain my sanity. You can pick any name you like. I’m going to provide examples/downloads in both C# and UnityScript/JavaScript.

You can get rid of the Start() and Update() functions, and we’ll be creating a single function called Execute(). I like to do this for all my scripts for purposes of polymorphism, but you can name them whatever you want. In this case you could simply call it Quit() if you wanted. What is absolutely important, however, is that you follow a few rules:

  1. It must return a void
  2. It can only accept a single basic variable type (e.g. int, string, boolean, GameObject, or float), so no enums or custom types*
  3. Cannot make calls to yield inside anything being called directly.**
* You can create and use a convenience class to pass as a single argument, for a work-around.
** This is due to how co-routines operate in the Unity engine. Supposedly a work-around can be accomplished by placing any yield calls ( e.g. yield WaitForSeconds(3); ) in a separate function, and then call that function from the primary function you’re calling.
C# ButtonClickQuit.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ButtonClickQuit : MonoBehaviour
{
    public void Execute()
    {
        Application.Quit();
    }
}
UnityScript ButtonClickQuit.js

#pragma strict

public class ButtonClickQuit extends UnityEngine.MonoBehaviour
{
    public function Execute() : void
    {
        Application.Quit();
    }
}

If you have pre-existing methods you want called attached to other objects, that’s no problem, so long as you follow the three rules I listed before. So, it may be necessary to use some of those work-arounds I mentioned before. Even for the first rule, you could simply create another container/utility function that calls what you need called and returns void.


Adding Functionality to Button

So we have a button in the GameObject hierarchy. In this example, I have a Canvas, containing a CanvasGroup GameObject named Bottom Bar. That one contains my Button which I named Button Quit.

 

So its a rather simple setup. And as we can see in the Inspector, there is a Button (Script) section where the various attributes like button colors can be changed. There is a also the empty OnClick() list at the bottom of the section.

Unity Button Script for Quit Button
Button (Script) Section in Inspector

 

My next step was to Add Component and create a new script which I called ButtonClickQuit.

After which you should see the new script attached as an (empty) new section in the inspector.

Adding New Script to Button in Unity 5
Creating the new ButtonClickQuit Script

Afterwards we’ll look back at the OnClick() list and you’ll see at the bottom right, there is a + / – pair of buttons where we can add our new script to be called. So I clicked the + to add a new list item.

By default you’ll see the drop-down has Runtime Only selected. You can leave that alone (the editor mode doesn’t refer to when you run the application inside of Unity, but rather to when you’re adding UI functionality to Unity itself. So unless you’re writing mods/scripts for Unity, then you can safely leave that alone.

Then directly underneath that we’ll drag and drop the button ( Button Quit ) onto this. This is done for two reasons. The first reason is because the functionality is attached to our button, so that’s how we’re going to access it. I know it seems redundant, but if the function or method you want to call is attached to another GameObject, then you can drag and drop that one on there instead.

Once you’ve done that, then the Function drop-down list will become populated with all of the viable public functions, booleans, or variables that you can modify or call.

For example, if all you wanted a button to do was simply set a boolean/int/string, then you could just select that and then type in what you want it to change to. There isn’t much more it offers beyond that, which is why anything beyond that requires a function call.

For our example though, we’ll scroll down the Function drop-down list to ButtonClickQuit → and then we can see our Execute() function and select that.

Voila! Now the new Quit button will quit the application when click on it.

Leave a Reply