Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

362
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda