How to lose currency when u buy something?

Hello

i am making a game in unity 2d (using c#)

i have 2 objects Currency and Shop

u get gold buy clicking on currency (script in Currency) and when u click on an item on the shop (script in Shope)you lose 5 gold.

how can i tell Currency that my gold is decreased by 5?

sorry i know it stupid question

edit#1:

lets say i have this script on my currency:

  void OnMouseDown ()
    {
    if (gameObject.name == "aaa") // when i click on a cube i get gold
    gold = gold +1
    }

and this for the shop

void OnMouseDown()
{
GameObject access = GameObject.Find("aaa");
Currency gg = access.GetComponent();

		int GoldIHave = gg.gold;

if (gameObject.name=="bbb" && Cost<=GoldIHave) // when i press a circle i buy something

//buy it
}

ok now how i tell the currency script that i bought something and lose gold equal to the cost?

2 Answers

2

gold -= 5;

or gold = gold - 5

if (gameObject.name == “bbb” && Cost <= GoldIHave)
{
gg.gold -= Cost;
}

If your object name is “bbb” and the Cost is less than or equal to the gold you currently have. Then subtract the Cost amount from your current gold within the Currency instance.

Is that what your after?

sry forgot to write < instead of > but how to send that code to the currency script ? (this is my problem)

Currency gg = access.GetComponent<Currency>(); This line assigns the instance of your Currency script to the variable 'gg'. To access the Currency scripts variables and methods, to modify them or use them you simply have to use the 'gg' variable. gg.gold -= Cost; This line is accessing the 'gold' variable within the Currency script instance you assigned to the variable 'gg' and modifying it to reflect that you have subtracted the Cost amount of an item from the current amount of gold in the Currency script. Does this help?