From d3387a88c943f56e941ab403e81ec2789e735997 Mon Sep 17 00:00:00 2001 From: dhain Date: Fri, 3 Jun 2022 09:36:03 +0200 Subject: [PATCH] fixed instance of object is null error when buying items in shop --- Assets/Scripts/ShopSlot.cs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/Assets/Scripts/ShopSlot.cs b/Assets/Scripts/ShopSlot.cs index f18347e..b34cf12 100644 --- a/Assets/Scripts/ShopSlot.cs +++ b/Assets/Scripts/ShopSlot.cs @@ -14,7 +14,7 @@ public class ShopSlot : ItemStorageSlot { _inventory = Inventory.instance; _playerController = PlayerController.instance; } - + /** * Clears the Shop Slot */ @@ -30,17 +30,21 @@ public class ShopSlot : ItemStorageSlot { * Gets called when the Shop Slot is clicked */ public override void UseItem() { - if(_playerController.Money >= Item.cost) { - _inventory.AddItem(Item, 1); - _shop.RemoveItem(Item, 1); - _playerController.ChangeMoney(-Item.cost); - - Debug.Log("Buying Item: " + Item.displayName); - } else { - Debug.Log("Not enough money to buy item."); + if(Item) { + if(_playerController.Money >= Item.cost) { + _inventory.AddItem(Item, 1); + _shop.RemoveItem(Item, 1); + if(Item) { + _playerController.ChangeMoney(-Item.cost); + + 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(); } -} \ No newline at end of file +}