#Question on Grabbing Variable Names

1 messages · Page 1 of 1 (latest)

fleet tulip
#

Hey guys! I am setting up an item shop for my game. I'm trying to create it completely from scratch using the skills I have learned so I haven't been watching a tutorial or anything like that so sorry if this is not the best way to go about this problem.

So I have an item shop where each button has a script attached that stores information like the cost of the item, the name, if its been unlocked, etc.. That script is unique to each button as I haven't hardcoded anything other than the variable names.

I then have a a game object that is the parent of all these buttons that has the main ItemShop Script that I will be referencing.

When a button is pressed, it access the parent game object that has the script attached and it calls the BuyItem() function. inside of that function. inside that function I try to acesss the cost and the name of the item. Again this information is inside the ItemShopInfo.cs script.

My question is, how do I, when pressing a button, call the BuyItem() function and tell it certain variables from the ItemShopInfo.cs script.

Here are some pictures in case my explanation is bad.

#

The script:

public void BuyItem()
    {
        itemName = GetComponent<ItemShopInfo>().name;
        cost = GetComponent<ItemShopInfo>().cost;
        
        purchaseText.text = $"Buy {itemName} for {cost} coins?";
        purchaseScreen.SetActive(true);
    }
#

I understand that the getcomponent solution would not work because obviously it is a couple children down from where this script is.

#

In the picture that shows the heirarchy, the main ItemShop.cs script is attached to the gameobject titled Item Shop. Then if you go way down to, for example, spring green, that is one of the buttons. Each button contains the ItemShopInfo.cs script that contains what I mentioned in the first post. In the first image, you can see that script being attached to the button itself.

topaz bear
#

You can have parameter in BuyItem

#

So the button give you it's ItemShopInfo to your BuyItem method when pressed

#

You are also better off when you set up from code side

fleet tulip
#

okay that kinda makes sense. what do you mean by parameter though?

topaz bear
#

public void BuyItem(ItemShopInfo info)