shop menu coins not incrementing

hello sir , i am new to unity and i have learned C# language actually i am making a game like flappy bird in which i have a shop menu i want to save coins in shop menu and i know for that i have to use playerprefsget int and setint . i am able to save the coins but after i play the level it save that coin in shop menu .

i mean to say its not incrementing like if i have 15 saved coins and if i play back and collect 16 coins
i get 16 coins only in my shop menu instead of 31…thats the makor problem i am facing .

please help me with my this problem . although my code is:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class purchase : MonoBehaviour
{

// private int currentvalue;
 private int gettingvalue;
 [SerializeField]
 private Text cointext;
int totalvalue;
int currentvalue ;


private void Awake()
 {
    addcoins();
   

}
 private void Update()
 {

//   

}
public void addcoins()
{
  
    
        gettingvalue = treetry.instance.finalscore;
        currentvalue += currentvalue + gettingvalue;
        totalvalue += currentvalue + totalvalue;
        cointext.text = totalvalue.ToString();
        PlayerPrefs.SetInt("totalcoins", totalvalue);
        PlayerPrefs.GetInt("totalcoins", totalvalue);
        PlayerPrefs.Save();
    

}

}

this code i have wriiten myself .

please help me your help is appreciated…
thank you in advance.

HI !! @truedreams !

First, where you say

     currentvalue += currentvalue + gettingvalue;
     totalvalue += currentvalue + totalvalue;

i think you are not doing what you really want. The symbol += means “add to current value”.

For example, if we have x=1 an do this:

x += 3;

Now x = 4. But you are not doing this, you are increasing itself + another value. In your code, you are doing this:

 x += x+3;

So now x=5.

So… i supose you wanted to do this

currentvalue +=  gettingvalue;
totalvalue +=  totalvalue;

I mean next 2 lines are exactly the same:

x = x + 3;
x += 3;

And if it increases only by one, next3 lines are the same:

x = x+1;
x += 1;
x ++ ;

In other hand…

What program are you using for scripting?

Do you know how to debug the code while running the game

Debug means, the code is monitored, and you can mark code line, and when the game try to read and execute that lines, it stops, and allow you to check the state of your variables, to see if they are as its supposed they has to be. This allow you to check if a method is called, or how the variables changes.

If you are having problems operating with variables, debugging is your best friend! If need more help just ask concrete questions and we will help you !

Remember to Accept / Upvote the answers ! And reply using @tormentoarmagedoom

Bye! :smiley:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class treetry : MonoBehaviour
{

public static treetry  instance;
[SerializeField]
private GameObject endingpanel;

[SerializeField]
private Text coinscollect;

[HideInInspector]
public int finalscore;

// private float countallcoins = 0;

private void Awake()
{
    makeinstance();
}

// Use this for initialization
void makeinstance()
{

    if (instance == null)
    {
        instance = this;
    }

}

// Update is called once per frame
void Update()
{
     coinscollect.text = finalscore.ToString(); // b1.coincount.ToString(); //+ countallcoins.ToString();
     PlayerPrefs.SetInt("coin", finalscore);
     finalscore = birdmovement.instance.coincount;

}

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.tag == "Player")
    {
        Destroy(collision.gameObject);
         endingpanel.SetActive(true);
       finalscore = birdmovement.instance.coincount;

       
       // mcpanel.SetActive(true);
    }
}

}