# increase and decrease coin count

I am working in coin system. if the player get the coin it adding to +1 coin. after game over i want to store the coins. if the player again earn coin means the new coin want to add with existing coins. anyone please help. Here's the Code i have used.

private int iCoin =0;
private int itotalCoin =0;
String TotalCoinKey = "TotalCoin";

void start(){
itotalCoin=PlayerPrefs.getInt(totalCoinKey,0);
}

void Update(){
playerprefs.Setint(TotalCoinKey, iCoin);
playerprefs.save();
}

public void Death(){
debug.log("current Level Coins = " +iCoin); //Show Coins earn in current level.
}


i want to save the earned coin and increment the coins.

• Please don't set/save player prefs in Update. You should move that to Death() . Update executes every frame. I/O operation every update is a bad idea. – SanSolo Dec 3 '15 at 11:44
• ok thanks and i want to store the coin value. in next game if the player again earn the coin means the new coin count want to add with existing coin count. how can i do that? – Manoj Balaji M Dec 3 '15 at 11:48
• The title says "increase and decrease coin count", but i don't see where you are decreasing the count. – SanSolo Dec 3 '15 at 11:50

void Update(){
playerprefs.Setint(TotalCoinKey, iCoin);
playerprefs.save();
}


what that is doing is, it is setting total coins value to the number of coins the player has earned so far this session.

Instead, use trigger for collision detection do detect when player collides with a coin. Using OnTrigger method, you can increment the iCoin every time player collides with a coin.

Then, when player dies,

public void Death()
{
itotalCoin=PlayerPrefs.getInt(totalCoinKey,0);
itotalCoin+=iCoin;
playerprefs.Setint(TotalCoinKey, itotalCoin);
playerprefs.save();
}


Update : How to determine whether to increase or decrease count.

All Collision methods can take optional parameter to identify the collided object and perform necessary action. In this case, you add tags to your coins e.g: tag inc for positive coins and tag neg for negative coins. Then, in OnTrigger method,

void OnTrigger(Collider col)
{
if(col.gameObject.tag=="inc")
iCoin++;
else
iCoin--;
}


I would also strongly advice to go through Unity documentation and tutorials to get a thorough understanding of methods. Also, note that there are two different physics available : Physics and Physics2D. Please refer docs and tutorials to efficiently use the one you need.

• i have placed two box in my game first is positive, Second is Negative. if the player object is triggered with positive means coins count wants to add +10, if the player object triggered with Negative means coin count wants to reduce as -10 how can i do that? – Manoj Balaji M Dec 4 '15 at 7:31
• @ManojBalajiM updated my answer. – SanSolo Dec 5 '15 at 7:50
• iCoin++ or iCoin-- will increase or decrease only 1 coin. for 1 trigger enter i want to increase or decrease 10 coins. – Manoj Balaji M Dec 7 '15 at 7:01
• iCoin+=10; or iCoin-+10; you should study some programming. – SanSolo Dec 7 '15 at 7:02