-1
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – SanSolo Dec 3 '15 at 11:44
  • \$\begingroup\$ 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? \$\endgroup\$ – Manoj Balaji M Dec 3 '15 at 11:48
  • \$\begingroup\$ The title says "increase and decrease coin count", but i don't see where you are decreasing the count. \$\endgroup\$ – SanSolo Dec 3 '15 at 11:50
4
\$\begingroup\$
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.

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Manoj Balaji M Dec 4 '15 at 7:31
  • \$\begingroup\$ @ManojBalajiM updated my answer. \$\endgroup\$ – SanSolo Dec 5 '15 at 7:50
  • \$\begingroup\$ iCoin++ or iCoin-- will increase or decrease only 1 coin. for 1 trigger enter i want to increase or decrease 10 coins. \$\endgroup\$ – Manoj Balaji M Dec 7 '15 at 7:01
  • \$\begingroup\$ iCoin+=10; or iCoin-+10; you should study some programming. \$\endgroup\$ – SanSolo Dec 7 '15 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.