Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Keeps track of other inventories within collider2D trigger area
- // Can exchange money with other inventory by calling TradeMoney when another inventory
- // is within the area
- public class Inventory {
- public int money = 0;
- void OnTriggerEnter2D(Collider2D collider) {
- // Store reference to inventory within area
- Inventory otherInventory = collider.GetComponent<Inventory>();
- if(otherInventory != null) {
- connectedInventory = otherInventory;
- }
- }
- void OnTriggerExit2D(Collider2D collider) {
- // Remove reference to leaving inventory
- Inventory otherInventory = collider.GetComponent<Inventory>();
- if(otherInventory != null) {
- connectedInventory = null
- }
- }
- public bool TradeMoney(int amount) {
- if(connectedInventory) {
- connectedInventory.money += amount;
- money -= amount;
- // Success
- return true;
- } else {
- // No connected inventory found so can't trade money
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement