---
description: Simple Panel with Buttons to Purchase Items
---

# BuyItem

This section provides a simple example of creating a UI-Panel with 2 UI-Buttons and UI Text to allow selection of items to purchase.&#x20;

The images below show how to configure the UI-Panel by adding UI-Buttons as children, and a UI text element to give details about the purchase.&#x20;

The BuyItem script has public methods that can be configured to be executed by a UI-Button in the inspector.&#x20;

In order for a method to be configured to be executed by a Button, it can not have more than 1 input parameter, so in this case, by defining the method to accept a string, then logic in the BuyItem script could determine what  is purchased.  Using a Switch-case statement simplifies the logic, but nested if-else logic could also be used.  The case: "robot", case: "Robot" provides a way to allow either spelled version to be considered a match.  When the case "robot" is matched, then the GameData.Score value is checked to see if there are enough funds to purchase the robot.  Similar to the case for Plasma. &#x20;

Important: Note that when using a string value in the inspector, you should not use quotation marks.&#x20;

The logic in Start( ) checks to see if the key "RobotState" has value "companion" , if that is true, then the Robot Purchase Button is deactivated.

![](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgOacVpbicrqehO%2F-MM7roDYiQV2goG9Zixh%2F-MM7sq37Iau7QkV9eY_G%2FScreen%20Shot%202020-11-14%20at%204.29.39%20PM.png?alt=media\&token=4326c731-712e-4a13-897c-6c6bc220df75)

![](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgOacVpbicrqehO%2F-MM7roDYiQV2goG9Zixh%2F-MM7sswKq8iVxwC1Dg2P%2FScreen%20Shot%202020-11-14%20at%204.30.38%20PM.png?alt=media\&token=9e5fba6f-2faa-4812-a627-421db4362e16)

![](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgOacVpbicrqehO%2F-MM7roDYiQV2goG9Zixh%2F-MM7svm-Z8ZhArAPZjPH%2FScreen%20Shot%202020-11-14%20at%204.30.50%20PM.png?alt=media\&token=9b3e29b1-2a6e-4ac2-aeff-981c2235af7e)

![](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgOacVpbicrqehO%2F-MM7roDYiQV2goG9Zixh%2F-MM7syf2yuwvzQliinlG%2FScreen%20Shot%202020-11-14%20at%204.31.13%20PM.png?alt=media\&token=7b4b2f06-3229-4644-927b-b0fa1a91aa02)

![](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgOacVpbicrqehO%2F-MM7roDYiQV2goG9Zixh%2F-MM7t0XVqm9-e5A5-X_L%2FScreen%20Shot%202020-11-14%20at%204.31.27%20PM.png?alt=media\&token=c9ba6dd7-4a93-4448-93d4-bbc755196e9f)

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

//to be added to a store panel
public class BuyItem : MonoBehaviour
{
    string newLine = System.Environment.NewLine;
    [SerializeField]
    Text displayText;

    [SerializeField]
    Button robotBtn, plasmaBtn;

    void Start()
    {
        displayText.text = "Purchase Items";

        //disable robot button if Robot is already a companion
        if( GameData.instanceRef.GetChoice("RobotState") == "companion")
        {
            robotBtn.gameObject.SetActive(false); //disable robot button
        }
    }

    public void PurchaseItem(string itemName)
    {
        int funds = GameData.instanceRef.Score;

        switch (itemName)
        {
            case "Robot":
            case "robot":
                if( funds >= 50)
                { 
                    GameData.instanceRef.Buy(50);
                    displayText.text = "Robot Purchased "  + newLine + "Score: " + GameData.instanceRef.Score;
                    GameData.instanceRef.SaveChoice("RobotState", "companion"); //set Robot as Companion
                    GameData.instanceRef.SaveChoice("LucyState", "growth"); //set LucyState
                    robotBtn.gameObject.SetActive(false); //disable robot button
                }
                else
                {
                    displayText.text = "Insufficient Funds, your account has: " + GameData.instanceRef.Score;
                }
                break;
            case "Plasma":
            case "plasma":
                if( funds >= 25)
                {
                    GameData.instanceRef.Buy(25);
                    GameData.instanceRef.BoostHealth(20);
                    displayText.text = "Plasma Purchased, Health is : " + GameData.instanceRef.Health + newLine + "Score: " + GameData.instanceRef.Score;
                }
                else
                {
                    displayText.text = "Insufficient Funds, your account has: " + GameData.instanceRef.Score;
                }

                break;

            default:
                displayText.text = "Your account balance is " + GameData.instanceRef.Score;

                break;
        }
    }
}

```
