fixed instance of object is null error when buying items in shop

This commit is contained in:
dhain 2022-06-03 09:36:03 +02:00
parent ccb25d4335
commit d3387a88c9

View file

@ -14,7 +14,7 @@ public class ShopSlot : ItemStorageSlot {
_inventory = Inventory.instance; _inventory = Inventory.instance;
_playerController = PlayerController.instance; _playerController = PlayerController.instance;
} }
/** /**
* Clears the Shop Slot * Clears the Shop Slot
*/ */
@ -30,17 +30,21 @@ public class ShopSlot : ItemStorageSlot {
* Gets called when the Shop Slot is clicked * Gets called when the Shop Slot is clicked
*/ */
public override void UseItem() { public override void UseItem() {
if(_playerController.Money >= Item.cost) { if(Item) {
_inventory.AddItem(Item, 1); if(_playerController.Money >= Item.cost) {
_shop.RemoveItem(Item, 1); _inventory.AddItem(Item, 1);
_playerController.ChangeMoney(-Item.cost); _shop.RemoveItem(Item, 1);
if(Item) {
Debug.Log("Buying Item: " + Item.displayName); _playerController.ChangeMoney(-Item.cost);
} else {
Debug.Log("Not enough money to buy item."); Debug.Log("Buying Item: " + Item.displayName);
}
} else {
Debug.Log("Not enough money to buy item.");
}
_shop.onItemChangedCallback?.Invoke();
_inventory.onItemChangedCallback?.Invoke();
} }
_shop.onItemChangedCallback?.Invoke();
_inventory.onItemChangedCallback?.Invoke();
} }
} }