Question by
yassinhakim700 · Apr 21, 2020 at 01:52 AM ·
playerprefscoins
my coins wont save when collected (USING PLAYERPREFS)
so whenever i collect a coin i made a script to destroy the object in an void ontriggerexit and to make a sound and a to run a method in another script here is my scripts: this script is attached to the coin in the level1 scene
using UnityEngine;
using UnityEngine.UI;
public class CoinScript : MonoBehaviour {
public AudioSource audio;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player")){
audio.Play();
GetComponent<Menu>().Coinbar();
}
}
private void OnTriggerExit(Collider other)
{
Destroy(gameObject);
}
}
and this script is attacked to a canvas in the MENU screen :
private void Update() {
coinbar.text = PlayerPrefs.GetInt("coins2").ToString();
}
public void Coinbar()
{
coins2 = PlayerPrefs.GetInt("Coins") + 1;
}
Comment
Answer by kbop2000 · Apr 21, 2020 at 02:23 AM
used the "PlayerPrefs.SetInt()" ?
public void Coinbar()
{
PlayerPrefs.SetInt("Coins", PlayerPrefs.GetInt("Coins") + 1);
}
Your answer
Follow this Question
Related Questions
Problem with coin script and operands 1 Answer
How do I save my coins with PlayerPrefs? 1 Answer
Coin Spending/PlayerPref Subtracting 0 Answers
Faster Way to Increment PlayerPrefs Int 1 Answer
Playerprefs, i dont know how to use 0 Answers