- Home /
My playerprefs wont save and load my int to TMPro text
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Currency : MonoBehaviour {
public int tokens;
public int index;
GameObject currencyUI;
GameObject currencyU;
void Start()
{
currencyUI = GameObject.Find("Currency");
currencyU = GameObject.Find("Currency");
currencyUI.GetComponent<TextMeshProUGUI>().text = PlayerPrefs.GetInt("Tokens", 0).ToString();
}
// Update is called once per frame
void Update()
{
currencyUI.GetComponent <TextMeshProUGUI> ().text = tokens.ToString();
if (tokens < 0)
{
tokens = 0;
}
PlayerPrefs.SetInt("Tokens", tokens);
}
}
This is the first script that makes it so that the currency is actually going up and being put on the game then the next script is meant to allow it so when you pick the coins up it adds it to the players token count but iv been trying for hours on end to make the script so that i can save it using something possibly even playerprefs but it just dosent seem to be working
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class PickupMoney : MonoBehaviour
{
Currency script;
private GameObject currencyU;
public int addAmount;
void Start()
{
script = GameObject.FindWithTag("GameController").GetComponent<Currency>();
currencyU = GameObject.Find("Currency");
//Currency saver \/
currencyU.GetComponent<TextMeshProUGUI>().text = PlayerPrefs.GetInt("Tokens", 0).ToString();
PlayerPrefs.SetInt("Tokens", script.tokens);
}
private void Update()
{
// currencyU.GetComponent<TextMeshProUGUI>().text = PlayerPrefs.GetString("tokens");
//PlayerPrefs.GetString("tokens", currencyU.GetComponent<TextMeshProUGUI>().text);
PlayerPrefs.SetInt("Tokens", script.tokens);
currencyU.GetComponent<TextMeshProUGUI>().text = PlayerPrefs.GetInt("Tokens", 0).ToString();
PlayerPrefs.Save();
}
void OnTriggerEnter(Collider obj)
{
if (obj.gameObject.tag == "Player")
{
script.tokens += addAmount;
Destroy(gameObject);
//Currency saver \/
}
}
}
if anyone knows the problem or wants to help but needs more information please feel free to ask any help is appreciated thanks for everyone help and time :)
Answer by RadonRaph · Feb 10, 2019 at 11:42 AM
Hello @gpuelston2004, i thin you should try somethign like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Currency : MonoBehaviour {
public int tokens;
GameObject currencyUI;
// Use this for initialization
void Start () {
//We set the var tokens with the amount of tokens from previous sessions
tokens = PlayerPrefs.GetInt("Tokens", 0);
currencyUI = GameObject.Find("Currency");
}
// Update is called once per frame
void Update () {
currencyUI.GetComponent<TextMeshProUGUI>().text = tokens.ToString();
if (tokens < 0)
{
tokens = 0;
}
}
public void AddTokens(int amount)
{
tokens += amount;
PlayerPrefs.SetInt("Tokens", tokens);
PlayerPrefs.Save();
}
}
public class PickUpMoney : MonoBehaviour
{
GameObject CurrencyManager;
Currency currency;
public int addAmount;
void Start()
{
CurrencyManager = GameObject.Find("CurrencyManager");
currency = CurrencyManager.GetComponent<Currency>();
}
void OnTriggerEnter(Collider obj)
{
if (obj.gameObject.tag == "Player")
{
currency.AddTokens(addAmount);
Destroy(gameObject);
}
}
}
All your tokens are stored in a var named tokens in the "Currency" script. At each start the tokens's var initialize with the previous value of previous session. If you want to modifiy the amount of tokens create a public function that modify the amount and save it in the playerPrefs.
Your answer
Follow this Question
Related Questions
Script with PlayerPrefs is not working on a different computer 1 Answer
Unity Character Selection + store 1 Answer
Destroy and Object and PlayerPrefs 1 Answer
How to stop objects you picked up from reappearing when you go to another level and return 1 Answer
How do I get my endless runner high score,How do I make high score for an endless runner 1 Answer