Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

349
Views
Problem with saving coins in Unity with PlayerPrefs

I am using PlayerPrefs to save collected coins on each level. In a differences game, when an item is found, player gets one coin and it is saved with PlayerPrefs. And that coin value is used to display in Level Selection screen. The problem is when i finished the level for the first time, it displayed collected coins in Level Selection screen, when i finished it another a couple of times, the value on the Level Selection screen, stayed the same, coins did not add up:

Here is my script where i save coins with PlayerPrefs:


if (result.collider.CompareTag("Images"))
                {
                    Debug.Log("Hit");
                    result.collider.enabled = false;
                    tmpCircle = Instantiate(krug1);
                    tmpCircle.SetActive(true);
                    tmpCircle.transform.SetParent(canvas.transform, true);
                    tmpCircle.transform.position = new Vector3(result.point.x, result.point.y, result.point.z);
                    tmpCircle.transform.localScale = new Vector3(transform.localScale.x * 2f, transform.localScale.y * 2f, transform.localScale.z);
                    circle.Play("CircleAnim");
                    score += 1;
                    diff -= 1;
                    coins += 1;
                    score1.text = " " + score;
                    PlayerPrefs.SetInt("Coins", coins);
                    PlayerPrefs.Save();
                    item.Play();
                }

And here is the script where i display the coins in LevelSelection Screen:


void Start()
    {
        coins.text = PlayerPrefs.GetInt("Coins", 0).ToString();        
    }

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Unless it is somewhere else in your code, you are setting your coins with each run of the level without reading it first. Every time you start the level you should read the coin data first or it will be overriden.

At the start of the level you should include

void Start()
{
    coins= PlayerPrefs.GetInt("Coins", 0);        
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!