- Home /
Need help with using Player Prefs to save number of coins collected.
I'm using unity 3D to make a game where the character collides with coins in order to collect them. When playing the game, the player should start out with zero coins and each time he collides with a coin, the amount should increase by one. On a main menu scene, the total number of coins (that have been collected across multiple games) should be displayed. The total number of coins is being displayed on the main menu but once in the gameplay scene, the total number of coins is being displayed there too (where it's supposed to start at 0 and then increase by 1 each time a coin is collected). Here is the script I use for when the player collides with a coin:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinScript : MonoBehaviour
{
void OnTriggerEnter(Collider target)
{
CoinScoreManager.coinAmount += 1;
Destroy(gameObject);
}
}
Here is the script I use to manage the UI and coin number in the game play scene:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinScoreManager : MonoBehaviour
{
public static int coinAmount;
public Text coinText;
void Start()
{
coinAmount = PlayerPrefs.GetInt("CoinAmount");
}
void Update()
{
coinText.text = coinAmount.ToString();
PlayerPrefs.SetInt("CoinAmount", coinAmount);
}
}
Here is the script I use to manage the UI and total coin number in the scene with the main menu:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MainMenuController : MonoBehaviour
{
int coinAmount;
public Text coinAmountText;
void Start()
{
coinAmount = PlayerPrefs.GetInt("CoinAmount");
}
void Update()
{
coinAmountText.text = coinAmount.ToString();
}
}
I think the problem is in the second script shown above. In the Start function I'm calling the coinAmount which is the same variable used to show the total coin number. I think that's the problem but I'm not really sure how to fix the problem as I'm still kinda of new to Unity and programming. Please let me know if there's anything else you need to know about the game in order to figure out how to fix this. Thanks for your help!
Answer by hdr25 · Jul 16, 2020 at 06:16 AM
@TaylorReich In CoinScoreManager script, you are getting totalcoins via playerprefs and starting from it, you need to start it from 0.
void Start()
{
coinAmount = 0;
}
void Update()
{
coinText.text = coinAmount.ToString();
}
And i would not recommend to save PlayerPref in Update() as it updates every frame and will add CoinAmount to playerpref every frame.
Create another function for example UpdateCoins() and call it everytime you collide with coin.
void UpdateCoins()
{
//This will update coins in PlayerPrefs.
PlayerPrefs.SetInt("CoinAmount"+PlayerPrefs.GetInt("CoinAmount")+ coinAmount );
}
It reports me an error regarding "PlayerPrefs.SetInt (" CoinAmount "+ PlayerPrefs.GetInt (" CoinAmount ") + coinAmount));" this line of code
It reports me an error regarding "PlayerPrefs.SetInt (" CoinAmount "+ PlayerPrefs.GetInt (" CoinAmount ") + coinAmount));" this line of code
The error tells me "Sets the value of the preference identified by key" how come?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to make my game settings save? 1 Answer
Distribute terrain in zones 3 Answers
Why does PlayerPrefs save to the Windows Registry? 0 Answers