# 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.

![](/files/-MM7sq37Iau7QkV9eY_G)

![](/files/-MM7sswKq8iVxwC1Dg2P)

![](/files/-MM7svm-Z8ZhArAPZjPH)

![](/files/-MM7syf2yuwvzQliinlG)

![](/files/-MM7t0XVqm9-e5A5-X_L)

```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;
        }
    }
}

```


---

# Agent Instructions: 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/overview-branchlogic/playerstats-v2/buy.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.
