I have made a coin system (in Unity C#) in the main menu,so I can buy items for them. I would like to pick up the coins in the game scene and add to this score,what I can see in my main menu and in my game scene too.
In the GameManager:
public Text coinText;
PlayerController Script:
gameManagerScript.coinText.text = "Currency : " + MainGameManager.Instance.currency.ToString();
gameManagerScript = GameObject.Find("GameManager").GetComponent<GameManager>();
When the player pick up the coin : `
private void OnTriggerEnter(Collider other)
{
Destroy(other.gameObject);
MainGameManager.Instance.currency += 1;
gameManagerScript.coinText.text = "Currency : " + MainGameManager.Instance.currency.ToString();
}
in the hierarchy I have made a UI -> Text and have dropped in the GameManager.
What I see in the game scene is : Currency: but I don't see how many I have and not add to the currency.
You can use static fields. In your GameManager:
public static int coins;
In Player:
GameManager.coins += val
You can also access the value using GameManager.coins in MainMenu script. Note:If you're trying to make this data persistent for different runs you can use PlayerPrefs class: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html