Null ref in Item when equiping in third person view

kretoskar

New member
Hi! I was following the tutorial on Ultimate Inventory System and Ultimate Character Controller integration. I have tried to follow it 3 times and every time I get the same error.

When it triggers:
1. When trying to pickup an item with auto equip in third person view
2. After changing the perspective from FPV to TPV when having an item equiped

The error:
On pickup

Code:
NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Items.Item.IsActive () (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Item.cs:648)
Opsive.UltimateCharacterController.Items.Item.set_ShowFullScreenUI (System.Boolean value) (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Item.cs:148)
Opsive.UltimateCharacterController.Items.Item.Equip (System.Boolean immediateEquip) (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Item.cs:501)
Opsive.UltimateCharacterController.Inventory.InventoryBase.EquipItem (Opsive.Shared.Inventory.IItemIdentifier itemIdentifier, System.Int32 slotID, System.Boolean immediateEquip) (at Assets/Opsive/UltimateCharacterController/Scripts/Inventory/InventoryBase.cs:350)
Opsive.UltimateCharacterController.Character.Abilities.Items.EquipUnequip.ItemEquip (System.Int32 slotID, System.Boolean canUpdate) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/Abilities/Items/EquipUnequip.cs:1001)
Opsive.UltimateCharacterController.Character.Abilities.Items.EquipUnequip.Update () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/Abilities/Items/EquipUnequip.cs:665)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.UpdateAbilities (Opsive.UltimateCharacterController.Character.Abilities.Ability[] abilities) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:794)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.UpdateUltimateLocomotion () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:725)
Opsive.UltimateCharacterController.Character.CharacterLocomotion.Move (System.Single horizontalMovement, System.Single forwardMovement, System.Single deltaYawRotation) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/CharacterLocomotion.cs:477)
Opsive.UltimateCharacterController.Game.KinematicObjectManager+KinematicCharacter.Move (System.Boolean manualMove) (at Assets/Opsive/UltimateCharacterController/Scripts/Game/KinematicObjectManager.cs:237)
Opsive.UltimateCharacterController.Game.KinematicObjectManager.Update () (at Assets/Opsive/UltimateCharacterController/Scripts/Game/KinematicObjectManager.cs:841)

Every frame after pickup:


Code:
NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Items.Item.IsActive () (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Item.cs:648)
Opsive.UltimateCharacterController.Character.ItemHandler.FixedUpdate () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/ItemHandler.cs:43)

Result:
On pickup in First person view everything works as expected. The item shows up correctly on the player
1626266897714.png

On pickup in third person view the item doesn't show on the player. The player animator is updated correctly.
1626267050614.png


Some debuging:
  • Awake in Item.cs was called
  • Item.cs lines 235-243
C#:
for (int i = 0; i < perspectiveItems.Length; ++i) {
    // Initialize the perspective item manually to ensure an Object GameObject exists. This is important because the Item component will execute
    // before the FirstPersonPerspectiveItem component, but the FirstPersonPerspectiveItem component may not be completely initialized.
    // The FirstPersonPerspectiveItem component must be initialized after Item so Item.Start can be called and add the item to the inventory.
    Debug.Log("Perspective items length - " + perspectiveItems.Length); //prints "2"
    if (!perspectiveItems[i].Initialize(m_Character)) {
        Debug.Log("Skipping");    //Is called once
        continue;
    }
 
Thank you for the detailed post.

Thanks to your logs we know that one of the perspective (which I assume is the third person perspective) is no being initialized correctly.
Looking at the code in more detail I see that that can only happen if the Item "Spawn Parent" cannot be found.

In the "ThirdPersonPersective" script find the "GetSpawnParent" function, This should not return null.
If it retruns null that means you are most probably missing an ItemSlot component on your player (or you have the wrong ID) meaning your weapon does not know where to spawn.

Just to be sure which one is the problem you can log things again:
Code:
protected override Transform GetSpawnParent(GameObject character, int slotID, bool parentToItemSlotID)
{
    
    Transform parent = null;
    var itemSlots = character.GetComponentsInChildren<ItemSlot>(true);
    Debug.Log("looking for slot ID: "+slotID);
    Debug.Log("number of slots: "+itemSlots.Length);
    
    for (int i = 0; i < itemSlots.Length; ++i) {
        Debug.Log("Found a slot with ID: "+itemSlots[i].ID)
        if (itemSlots[i].ID == slotID) {
            parent = itemSlots[i].transform;
            break;
        }
    }
    return parent;
}

I hope this helps fix your issue
 
I get similar errors when switching from a third person character to a first person character that has items equipped.
Code:
NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Items.Item.IsActive () (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Item.cs:639)
Opsive.UltimateCharacterController.Character.ItemHandler.Update () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/ItemHandler.cs:43)
These bugs keep popping up every frame, and equipped weapons don't work properly.
 

The video shows how I start with a first person character, everything works fine, I change to a third person character, everything works fine, but when I try to change back to a first person character, the described errors occur.
 
When you have an error, always start with the first one. Because anything after can be a cascading effect from the first error.

Thanks to your video I can see the "real" error is from the OnChangePerspective function. But I can't see the line where it occurs.
1661757663014.png

Looking at the code I'm wondering what could have gone wrong. Could you confirm that your project has the compile definition "FIRST_PERSON_CONTROLLER"?
If it's missing, that's the only reason I can think of that would give the error but then I'm surprised anything related to first person would work at all. You should be able to tell by opening the code in your IDE. If you aren't sure if it is add a Debug.Log at line 1060 in the Item script (Character Item not Inventory Item)
Usually the compilation define should have been added automatically on install, but it can also be added manually in the Project settings.
1661758035095.png
 
When you have an error, always start with the first one. Because anything after can be a cascading effect from the first error.

Thanks to your video I can see the "real" error is from the OnChangePerspective function. But I can't see the line where it occurs.
View attachment 9502

Looking at the code I'm wondering what could have gone wrong. Could you confirm that your project has the compile definition "FIRST_PERSON_CONTROLLER"?
If it's missing, that's the only reason I can think of that would give the error but then I'm surprised anything related to first person would work at all. You should be able to tell by opening the code in your IDE. If you aren't sure if it is add a Debug.Log at line 1060 in the Item script (Character Item not Inventory Item)
Usually the compilation define should have been added automatically on install, but it can also be added manually in the Project settings.
View attachment 9503
Sorry, didn't notice the first error.
Code:
NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Items.Item.OnChangePerspectives (System.Boolean firstPersonPerspective) (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Item.cs:1072)
Opsive.Shared.Events.InvokableAction`1[T1].Invoke (T1 arg1) (at <6ee9d952a9384b4ea988fa1f7efc55d9>:0)
Opsive.Shared.Events.EventHandler.ExecuteEvent[T1] (System.Object obj, System.String eventName, T1 arg1) (at <6ee9d952a9384b4ea988fa1f7efc55d9>:0)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.OnAttachLookSource (Opsive.UltimateCharacterController.Character.ILookSource lookSource) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:643)
Opsive.Shared.Events.InvokableAction`1[T1].Invoke (T1 arg1) (at <6ee9d952a9384b4ea988fa1f7efc55d9>:0)
Opsive.Shared.Events.EventHandler.ExecuteEvent[T1] (System.Object obj, System.String eventName, T1 arg1) (at <6ee9d952a9384b4ea988fa1f7efc55d9>:0)
Opsive.UltimateCharacterController.Camera.CameraController.InitializeCharacter (UnityEngine.GameObject character) (at Assets/Opsive/UltimateCharacterController/Scripts/Camera/CameraController.cs:493)
Opsive.UltimateCharacterController.Camera.CameraController.set_Character (UnityEngine.GameObject value) (at Assets/Opsive/UltimateCharacterController/Scripts/Camera/CameraController.cs:65)
SwitchCharacter.SwitchToHuman () (at Assets/_MY/SwitchCharacter.cs:40)
SwitchCharacter.Update () (at Assets/_MY/SwitchCharacter.cs:65)
Change the character like this:
Code:
cameraController.Character = humanCharacter; // Line 40
cameraController.SetPerspective(true);
On line 65, I just call the change character method.

"FIRST_PERSON_CONTROLLER" added.
 
Last edited:
Can you confirm that your item has both a First and Third person perspective component?
Send me a screenshot of you First person perspective. Also please confirm you have a FirstPersonObject on your character and that the Perspective points to the same ID ( 0 ).
1661845555657.png
1661845589289.png

In the Item Awake function at the end please add a Debug.Log(m_FirstPersonPerspectiveItem) to make sure it isn't null.
Then add another in the OnChangePerpectives so that you can double check which perspective it is trying to use. Or even better go in Debug mode and set a break point to step through the code
 
  • Like
Reactions: EVG
After adding the ThirdPersonPerspectiveItem component on the Item, the error no longer appears. I didn't know that the ThirdPersonPerspectiveItem component should be present on a weapon for a character that only uses first person view (Third person view is used by a completely different character without items).
Thanks for the help!
 
Ah I think that's a use case we didn't take into account.
So you have a first person character which is a UCC character, and then you have a Third person character which is another character controller? that doesn't need items? But you are still using the UCC camera for first and third?
Would you mind explaning your setup in a bit more detail, it would be interesting to know what your requirements are to see if its something we should take into account for a future update.
 
I have a separate character with a first-person view, he has weapons and other items, this is a person. There is also another character with a third-person view, he does not have any items, this is a snowman. The game has the ability to switch from one character (human) to another (snowman) and vice versa. Both characters use controllers from Opsive (FPC and TPC) They use the same camera controller.
 
Thanks, that makes sense, I'll add that use case in the things to review to make sure we're not missing anything.
 
  • Like
Reactions: EVG
Top