Runtime item pickup error

bbjones

Member
Simply following the tutorial
I am using TPS only, assault rifle, and it doesn't seem to matter if I use the demo rifle item or my own custom item.

I can create the item and pickup object fine, add to scene and set the Item Pickup script settings, but running the scene results in errors.
One thing to note is the item pickup object has all the correct scripts added, but I don't see the yellow bounding box gizmos as shows in the tutorial video.

In script ItemAction.cs - Awake()

This line fails: m_Character = characterLocomotion.gameObject;
Since characterLocomotion is null.

C#:
        protected override void Awake()
        {
            base.Awake();

            m_GameObject = gameObject;
            m_Item = m_GameObject.GetCachedComponent<Item>();
            var characterLocomotion = m_GameObject.GetCachedParentComponent<UltimateCharacterLocomotion>();
            m_Character = characterLocomotion.gameObject;

Error:

NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Items.Actions.ItemAction.Awake () (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Actions/ItemAction.cs:58)
Opsive.UltimateCharacterController.Items.Actions.UsableItem.Awake () (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Actions/UsableItem.cs:121)
Opsive.UltimateCharacterController.Items.Actions.ShootableWeapon.Awake () (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Actions/ShootableWeapon.cs:344)

I don't understand why the pickup item expects there to be a parent character?
I have a TPS character, with no items, that works fine by itself.
 
Last edited:
I was able to get this to partially work, but I still don't understand why things are different.

I created prefabs for Nolan and the AR pickup object from the demo scene, dropped those in my test scene and they work as expected.
I then compared the AR pickup prefab copy from the demo scene to the one I created following the tutorial video, they are very different objects as you can see below, essentially the one I created has many extra scripts.
AR pickup object from Demo Scene:
FromDemo-ARpickup.png
AR pickup object I made:
customARpickup.png

There are similar differences on the next child object (Assault Rifle and Third Person pickupTestitem), again my object has many extra scripts.

If I manually delete these extra scripts I can run the scene and perform a pickup on my custom object/item (although it only activates the demo AR item on Nolan since that matches the type I'm using).

The steps to both create a new custom item prefab, and matching pickup object are very simple so I don't see how I end up with extra scripts on both objects. Any ideas?
 
they are very different objects as you can see below, essentially the one I created has many extra scripts.
Those look like they are two different object types. The demo scene one was created with the Object Manager, located at this time in the video:


Whereas the pickup that you created is for the actual item. The pickup script should point to this item prefab.
 
Thanks for the reply, Justin.

What the problem seems to have been was I was using the item prefab (from creating a new item prefab via the Item Manager) in the GameObject slot when creating the Item Pickup Object, instead of just selecting the base item model. I find it difficult to distinguish between items when everything is named the same, "Sword" in the case of that tutorial.

While it seems I can now create a pickup item and run the scene without error, I'm encountering a series of errors that seem to be related to not having setup the character correctly to accept picking up the item at runtime, various errors about slots (details at the end of this post).
I haven't found documentation on how to deal with this using my own custom models:
  • create new item - WORKS
  • create new runtime pickup object for that item - WORKS
  • run scene with character with no items/inventory - WORKS
  • try to pickup item at runtime - DOESN'T WORK
  • create new item for character that matches the item I added for pickup - WORKS
    - I added the item with "Add to default loadout" unchecked, so the item exists under the character at the right nodes, but is disabled when the scene starts
  • try to pickup matching item at runtime - DOESN'T WORK
So far I haven't been able to find any documentation or guidance that covers this scenario. I know this works in the demo but I don't know how to setup a character of my own beyond the basics covered in the video tutorials.
I'm sure I can debug the code and figure it out eventually, but I'm hoping there is established guidance I can follow to save time.

Latest error details:
The pickup item object I created and placed in scene to then set the ItemPickupSet details:
CustomPickupItemObject.png

ItemSetManager.cs
C#:
        public void AddItemSet(Item item, ItemSet itemSet, bool defaultItemSet)
        {
            for (int i = 0; i < item.ItemType.CategoryIndices.Length; ++i) {
                var categoryIndex = item.ItemType.CategoryIndices[i];
                var addItemSet = true;
                var itemSetList = m_CategoryItemSets[categoryIndex].ItemSetList;
                for (int j = 0; j < itemSetList.Count; ++j) {
                    var slots = itemSetList[j].Slots;
                    var slotMatch = true;
                    for (int k = 0; k < slots.Length; ++k) {
                        if (slots[k] != itemSet.Slots[k]) { <<--- ERROR ON THIS LINE
                            slotMatch = false;
                            break;
                        }
                    }
                    if (slotMatch) {
                        addItemSet = false;
                        break;
                    }
                }


NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Inventory.ItemSetManager.AddItemSet (Opsive.UltimateCharacterController.Items.Item item, Opsive.UltimateCharacterController.Inventory.ItemSet itemSet, System.Boolean defaultItemSet) (at Assets/Opsive/UltimateCharacterController/Scripts/Inventory/ItemSetManager.cs:170)
Opsive.UltimateCharacterController.Objects.CharacterAssist.ItemPickup.DoItemPickup (UnityEngine.GameObject character, Opsive.UltimateCharacterController.Inventory.InventoryBase inventory, System.Int32 slotID, System.Boolean immediatePickup, System.Boolean pickupItemType) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/CharacterAssist/ItemPickup.cs:172)
Opsive.UltimateCharacterController.Objects.CharacterAssist.ItemPickup.TryItemPickup (UnityEngine.GameObject other, Opsive.UltimateCharacterController.Inventory.InventoryBase inventory, System.Int32 slotID) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/CharacterAssist/ItemPickup.cs:128)
Opsive.UltimateCharacterController.Objects.CharacterAssist.ItemPickup.TriggerEnter (UnityEngine.GameObject other, System.Int32 slotID) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/CharacterAssist/ItemPickup.cs:116)
Opsive.UltimateCharacterController.Objects.CharacterAssist.ItemPickup.TriggerEnter (UnityEngine.GameObject other) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/CharacterAssist/ItemPickup.cs:94)
Opsive.UltimateCharacterController.Objects.CharacterAssist.ObjectPickup.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/CharacterAssist/ObjectPickup.cs:164)
 
Ugh, noticed more problems...

The AR Pickup Object prefab I copied from the demo scene still has a different structure, note the AssaultRifle object is a child.
FromDemo-ARpickup.png

On the Pickup Object I created it looks like my weapon item is mixed with the pickup object itself.
1572286915450.png

So I'm assuming I'm still doing something wrong. Here's the steps I take:

  1. create TPS character with new custom item collection
  2. create item prefab using item types from my custom item collection, with my custom weapon model as the "Third Person Visible Item" - this model is the asset I purchased in a pack from the asset store, unmodified, it is a prefab that simply has a mesh renderer and some child objects for the weapon clip, trigger etc. It also works fine in other testing adding items to my Opsive TPS character as default loadout.
  3. create item pickup object, use the asset store weapon model as the GameObject
  4. place item pickup object prefab in scene, I end up with what you see in the image above for the pickupTest5 object
  5. I select the item pickup object, add a row to Item Pickup Set and drag the item prefab created in step 2 as the Item, this brings up the correct item type from the correct item collection assigned to the correct slot
  6. I add Item Type Counts for the weapon and ammo using the correct ItemCollection types
  7. Get errors noted in previous post trying to pickup item at runtime.
Not sure if that matters or not that I end up with different pickup object structures.
 
There is at least one solution to this now.
I can now create my own pickup items, and pick them up at runtime using a TPS character that was created with no items to start with, all using my own custom models and item collection/types.

I've had a few emails back and forth with Justin about this as the forums were being moved.

The last problem I was running into was due to what looks like a bug when trying to change the Item Collection when creating a new character.

The issues was I would create a Opsive TPS character and select the ItemCollection I created, not the DemoItemCollection.
But, the character in the scene hierarchy still had DemoItemCollection selected in the ItemSetManager script.
That was causing errors when trying to pickup the item because obviously the pickups I created were then from the wrong item collection.
Simply changing to the right item collection in ItemSetManager eliminated the errors.

Justin, this is a repeatable bug.

The first time you create a character and select a different Item Collection, it will create the character with whatever the old Item Collection was, requiring you to take an additional manual step of selecting the right Item Collection in ItemSetManager on the new created character.

UCC v2.1.8
Unity v2020.1.0a3 Personal
Steps:
  • Drag new character into scene
  • Create a new item collection
  • using Opsive Character manager, create new TPS character
  • Select the new item collection instead of the default DemoItemCollection
  • Build Character, accept ragdoll
  • Inspect character's ItemSetManager script, it will show DemoItemCollection when it should show the new item collection you created and selected when creating the character

Two other issues came up while dealing with this, if I get stuck I'll make a new post specific to the issue.
  • In simple testing I wouldn't bother to align the pickup item with the player since I was just trying to get through the pickup error. But, I was expecting I could at least fire the weapon once I got pickup working, but the weapon wouldn't fire. The problem there is you must align the weapon fairly close to where the character is looking to be able to fire. There are also other ways to get around that by changing scripts/values related to where and how the character looks, but I didn't want to mess with the default character setup too much.
  • The assets I'm using for weapons and characters has a scaling issue. I can pickup the item fine, but the weapon is scaled to 0.01 (because my root model is scaled to 0.01) so you can't really see it. I can scale the weapon up once attached to the character hand, but that isn't the best way to deal with it since you can't change the scale of an item on the item prefab, you can only change local position and rotation. Justin suggested I address this during asset import which I'm looking into https://docs.unity3d.com/Manual/FBXImporter-Model.html
Thanks for all of the help, Justin.
 
UCC v2.1.8
Unity v2020.1.0a3 Personal
Steps:
  • Drag new character into scene
  • Create a new item collection
  • using Opsive Character manager, create new TPS character
  • Select the new item collection instead of the default DemoItemCollection
  • Build Character, accept ragdoll
  • Inspect character's ItemSetManager script, it will show DemoItemCollection when it should show the new item collection you created and selected when creating the character
Thank you for posting this! I bet this has messed a lot of people up. I have it fixed - the Character Manager was using the existing Item Definition when trying to create the new character.
 
Top