- Home /
collectable currency doesn't transfer between different scenes
ok so basically I have two scripts to do with currency in my game at the moment, one which adds money when I collide with an object (like a coin) and deletes it. And one which shows how much money I have in a UI Element and the problem I have is that the money doesn't transfer between different scenes and I'm really struggling to find a way to fix this fairly easily
Currency Script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Currency : MonoBehaviour {
public int gold;
GameObject currencyUI;
void Start()
{
currencyUI = GameObject.Find("Currency");
}
void Update()
{
currencyUI.GetComponent<Text>().text = gold.ToString();
if (gold < 0)
{
gold = 0;
}
}
}
And the pick up money script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PickUpMoney : MonoBehaviour {
Currency script;
public int addAmount;
void Start()
{
script = GameObject.FindWithTag("GameController").GetComponent<Currency>();
}
void OnTriggerEnter(Collider obj)
{
if (obj.gameObject.tag == "Player")
{
script.gold += addAmount;
Destroy(gameObject);
}
}
}
i'd be very happy if someone could explain what I could do to be able to transfer the amount of money I have in one scene into another scene and back an forth etc.!! thank you