Bug in Health Pickup

After the Fighter and Equipment video, there has been a null reference error when trying to click on or pickup a health pickup. Like Sam, I used the pumpkin for it.

This is the null reference error showing up every time. To me, the error seems to have to do with stackable items. But the Health Pickup prefab wasn’t setup for it to be a stackable item.

NullReferenceException: Object reference not set to an instance of an object
RPG.Inventories.Inventory.FindStack (RPG.Inventories.InventoryItem item) (at Assets/Scripts/Inventories/Inventory.cs:207)
RPG.Inventories.Inventory.FindSlot (RPG.Inventories.InventoryItem item) (at Assets/Scripts/Inventories/Inventory.cs:177)
RPG.Inventories.Inventory.AddToFirstEmptySlot (RPG.Inventories.InventoryItem item, System.Int32 number) (at Assets/Scripts/Inventories/Inventory.cs:68)
RPG.Inventories.Pickup.PickupItem () (at Assets/Scripts/Inventories/Pickup.cs:55)
RPG.Control.ClickablePickup.HandleRaycast (RPG.Control.PlayerController callingController) (at Assets/Scripts/Control/ClickablePickup.cs:32)
RPG.Control.PlayerController.InteractWithComponent () (at Assets/Scripts/Control/PlayerController.cs:69)
RPG.Control.PlayerController.Update () (at Assets/Scripts/Control/PlayerController.cs:55)

The only thing that could be null at this particular location is the item itself. Does the picku phave it’s item assigned?

It’s the health pickup prefab from the video “Health Pickup hack” in the Core Combat course. And now that I’m in the Inventory course, in the Integrating the Inventory System, the Health Pickup prefab is not able to be picked up. It’s what is causing the null errors that I showed in the first message. And I can’t find a video in the Integrating videos to create a health item that would go in the inventory resources folder that when spawned and picked up, the players health would increase.

I think the old pickup hack isn’t compatible with this system.

You’ll need to create a script that is descended from ActionItem, something like this:

SimpleHealthPotion
using GameDevTV.Inventories;
using RPG.Attributes;
using UnityEngine;

namespace RPG.Abilities
{
    [CreateAssetMenu(fileName = "Simple Health Potion", menuName = "RPG/Simple Health Potion")]
    public class SimpleHealthPotion : ActionItem
    {
        [SerializeField] private float amount = 10f;
        
        public override bool Use(GameObject user)
        {
            if (user.TryGetComponent(out Health health))
            {
                health.Heal(amount);
            }
        }
    }
}

Then create a new Potion through the asset menu and link that to the Inventory System’s ClickablePickup.

Thanks Brian that worked wonders. It just needs tweaked a little in order to make small, medium and full health potions. Now got to figure out how to make manna potions.

Might I suggest our Shops and Abilities course for this… not only a better way to create abilities, but we cover mana and regeneration there as well.

Well that is going to be my next course I do. Thanks again for the help Brian.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms