Skip to content

Instantly share code, notes, and snippets.

@Dssdiego
Created April 1, 2020 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dssdiego/f732e6c40a03ccfa10daab1306336194 to your computer and use it in GitHub Desktop.
Save Dssdiego/f732e6c40a03ccfa10daab1306336194 to your computer and use it in GitHub Desktop.
Unity Action
using UnityEngine;
using UnityEngine.Events;
public class Money : MonoBehaviour
{
[SerializeField] private int amount = 10000;
public static UnityAction<int> OnGrabbed;
public void Grab()
{
OnGrabbed.Invoke(amount);
Destroy(gameObject);
// TODO: A Player can only pick X amount
}
}
using System;
using TMPro;
using UnityEngine;
public class MoneyHUD : MonoBehaviour
{
private TextMeshProUGUI moneyUI;
private int money;
void Start()
{
moneyUI = GetComponent<TextMeshProUGUI>();
money = 0;
moneyUI.SetText("" + 0);
Money.OnGrabbed += OnMoneyGrabbed;
}
private void SetMoneyAmount(int amount)
{
moneyUI.SetText($"{Convert.ToDecimal(amount) / 100:#.00}");
}
private void OnMoneyGrabbed(int amount)
{
money += amount;
SetMoneyAmount(money);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment