unity c# money system
I want this to program please help me im stock with this logic! example: i have a $10 dollar money and when playing the game i get score of $4 how can i add the $4 in the $10 money. How can i code that in unity c#
Answer by Margallo · Nov 25, 2016 at 12:54 PM
I'm sorry for my bad english @ dacarrera! that's why its not working the totalMoney and the money is reverse thanks :) but one more thing how can i save the totalMoney using playerprefs?
You'll want to use the SetInt()
method as seen in the docs here. Glad I was able to help answer your question! :)
Answer by dacarrera · Nov 25, 2016 at 11:01 AM
Hey Margallo,
An exact implementation for a money system will depend on the nature of your game. A simple way of doing this is to store your "money" as an int and to increase/decrease it as needed.
For example:
// Simple money system
public int playerMoney = 0; // Start with $0
// Function to increase money
// This should change based on the way you want to
// have the player make money
void addPlayerMoney(addAmount){
playerMoney += addAmount;
}
So now whenever addPlayerMoney() is called, it'll increased your money variable by addAmount. From here, it'll be up to you! For example, calling addPlayerMoney(5) will increase your money by 5, and playerMoney will now = 5.
I hope this helps!
Hi! thank you for helping i just did what you said but in my game i just want to make like for example
whenever it collide to the coins it score of +2 and when in game over i want it to have a total coin and the total text have save not in the coins text. i need a function to increase the save money by in total coins.the
coin.text + total.text something like that?
here is my code in money$$anonymous$$anager!
public static int money = 0;
public Text moneytxt;
public Text totalTxt;
public static int total$$anonymous$$oney;
void Update () {
moneytxt.text = "" + money;
}
void Total$$anonymous$$oney (){
money += total$$anonymous$$oney;
totalTxt.text = "" + total$$anonymous$$oney;
}
and in the coin Collision!
public int moneyToadd = 2;
void OnCollisionEnter(Collision col){
if(col.gameObject.tag == "Player"){
Destroy (gameObject);
sm.coinSound.Play ();
scoreCOin.money += moneyToadd;
}
}
Glad to be of some help. I'm slightly confused with the wording of your question, so please let me know if I'm going off track here. Right now I see that on collision, you'll add 2 to your money
from money$$anonymous$$anager. However, in your Total$$anonymous$$oney()
function, you're increasing the value of money
rather than total$$anonymous$$oney
. You should switch it around so that total$$anonymous$$oney is being increased --> total$$anonymous$$oney += money
. Is there anything else you're trying to add/implement?
Your answer
![](https://koobas.hobune.stream/wayback/20220612101401im_/https://answers.unity.com/themes/thub/images/avi.jpg)