> For the complete documentation index, see [llms.txt](https://kdoore.gitbook.io/cs2335/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kdoore.gitbook.io/cs2335/project1-code/minigamemanager/startbutton.md).

# StartButton

Button Component UnityEvent

### UI Buttons:  [Unity Manual](https://docs.unity3d.com/2018.3/Documentation/Manual/script-Button.html)

**UI Buttons** can be configured to **execute a public method** when the **Button's OnClick Event** occurs.

## Create UI Button GameObject

* Create a UI Button
* it will also have a child-gameObject that is UI Text
* Rename the Button to StartButton
* Set the RectTransform to position the StartButton.
* In the Inspector, make sure to set both Normal and Highlighted Colors
* Set Navigation to None
* Notice that OnClick( ) has:  List is Empty

![Create StartButton, Buttons Have a Child Text GameObject](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgOacVpbicrqehO%2F-MHSwSVeSQgyBX7zUkjD%2F-MHSyHTvqdnxfcWD55et%2FScreen%20Shot%202020-09-17%20at%204.26.23%20PM.png?alt=media\&token=a93420af-2568-47f9-b238-e6db53712da0)

<div align="center"><img src="https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgOacVpbicrqehO%2F-MHSwSVeSQgyBX7zUkjD%2F-MHSy7Ix2OwefFox-NZu%2FScreen%20Shot%202020-09-17%20at%204.26.03%20PM.png?alt=media&amp;token=2dad500c-5458-482a-8bed-f0cf3ae860b1" alt="Make Sure to Select a Highlighted Color that is different than the Normal Color"></div>

### UnityEvent:  onClick adds Listener Methods

UI-Button Components have an associated **UnityEvent:  onClick.**&#x20;

The Button Component can be considered as using the **Observer Pattern**.  The Observer pattern provides a method to **decouple** logic when 2 objects need to communicate with each other. &#x20;

In our case, the **Button** is the **Event Subject or Publisher,** our custom **MiniGameManager** script is the Event Listener. We must define a **public** **method** in the **MiniGameManager** class, to define a **Listener method** which will be configured to be executed when the **Publisher's onClick event is invoked.**&#x20;

**OnClick** is a **UnityEvent,** it maintains a list of all **Listener methods** to be executed when the **OnClick event is invoked**. The Listener object must define a public method to be added as a listener to the Button's onClick Event list of listeners. &#x20;

[**Game Programming Patterns:  Observer Pattern**](https://gameprogrammingpatterns.com/observer.html)

> The observer pattern lets one piece of code announce that something interesting happened *without actually caring who receives the notification*.

In the code below, we have defined a function that we want to have executed when the button OnClick() event occurs. :&#x20;

**`public void GameStartActions(){ //do game start things   }`** &#x20;

It is required that our function match the **onClick event's delegate-type**: in this case, **a public method with a void return-type and no input parameters**, in order to add our **GameStartActions()** method as a listener to the button's **onClick event.**&#x20;

&#x20;When the button object is clicked, we want our custom script component to be notified, we do so by creating a **public method that is passed as a delegate** to the button's onClick even&#x74;**.**  A **Delegate** provides a way to create a **reference-type** that **refers to a method** instead of a variable or object. Note, we're **passing a method** itself into the **AddListener( ) method** of the onClick event.

```csharp
[SerializeField]
Button startButton;

void Start(){
startButton = GetComponent<Button> ();
startButton.onClick.AddListener (GameStartActions);
    }

public void GameStartActions(){
    // do game start things
}
```
