Quantcast
Channel: Questions in topic: "playerprefs"
Viewing all articles
Browse latest Browse all 1333

How can I save the gold in my game scene to the main menu scene?

$
0
0
Hello I have a problem about PlayerPrefs. The golds in the game scene come to the main menu scene, but the previous golds are also deleted. For example; I won 5 gold from the first game, 5 gold will be written on the main menu scene. Let's say I won 4 gold in the second game, normally a total of 9 gold should be written on the main menu scene, but instead of deleting the previous gold, the gold I earned in the last game is written. So 4 will be written. How can I solve this problem? I need your help. I want all the gold to be collected in the main menu scene. My game scene: public class PlayerControl : MonoBehaviour { private float gold; public Text goldText; private void OnCollisionStay(Collision collision) { if (collision.gameObject.tag == "Gold") { goldText.text=((int)gold).ToString(); gold ++; PlayerPrefs.SetFloat("Gold", gold); PlayerPrefs.Save(); Destroy(collision.gameObject); } } } My main menu scene: public class Menu : MonoBehaviour { public Text goldText; public float gold; void Start() { PlayerPrefs.SetFloat("Gold",PlayerPrefs.GetFloat("Gold") + gold); gold=PlayerPrefs.GetFloat("Gold",gold); goldText.text="Gold: " + gold; }

Viewing all articles
Browse latest Browse all 1333

Trending Articles