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