> 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/f20_bkup_v2/project1-code/minigamemanager/startbutton.md).

# StartButton

### 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](/files/-MHSyHTvqdnxfcWD55et)

<div align="center"><img src="/files/-MHSy7Ix2OwefFox-NZu" 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
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kdoore.gitbook.io/cs2335/f20_bkup_v2/project1-code/minigamemanager/startbutton.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
