• We are looking for a content creator to help market our assets and a future unannounced project. More details are on this page.

Attacker GameObject is null when OnDeath is raised

Hi,

For some reason, whenever the OnDeath event is raised, the attacker GameObject is always null:

Code:
NullReferenceException: Object reference not set to an instance of an object
Game.Core.Managers.CharacterManager.OnDeath (UnityEngine.Vector3 position, UnityEngine.Vector3 force, UnityEngine.GameObject attacker) (at Assets/Scripts/Managers/CharacterManager.cs:124)
Opsive.Shared.Events.InvokableAction`3[T1,T2,T3].Invoke (T1 arg1, T2 arg2, T3 arg3) (at <27da9e1afec54f2fb2a11d46a234f9df>:0)
Opsive.Shared.Events.EventHandler.ExecuteEvent[T1,T2,T3] (System.Object obj, System.String eventName, T1 arg1, T2 arg2, T3 arg3) (at <27da9e1afec54f2fb2a11d46a234f9df>:0)
Opsive.UltimateCharacterController.Traits.Health.Die (UnityEngine.Vector3 position, UnityEngine.Vector3 force, UnityEngine.GameObject attacker) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Traits/Health.cs:493)
Opsive.UltimateCharacterController.Traits.CharacterHealth.Die (UnityEngine.Vector3 position, UnityEngine.Vector3 force, UnityEngine.GameObject attacker) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Traits/CharacterHealth.cs:97)
Opsive.UltimateCharacterController.Traits.Health.OnDamage (Opsive.UltimateCharacterController.Traits.Damage.DamageData damageData) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Traits/Health.cs:410)
Opsive.UltimateCharacterController.Traits.CharacterHealth.OnDamage (Opsive.UltimateCharacterController.Traits.Damage.DamageData damageData) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Traits/CharacterHealth.cs:81)
Opsive.UltimateCharacterController.AddOns.Multiplayer.PhotonPun.Traits.PunHealthMonitor.OnDamageRPC (System.Single amount, UnityEngine.Vector3 position, UnityEngine.Vector3 direction, System.Single forceMagnitude, System.Int32 frames, System.Single radius, System.Int32 originatorPhotonViewID, System.UInt32 hitColliderID, System.Int32 hitItemSlotID) (at Assets/Plugins/Opsive/UltimateCharacterController/Add-Ons/Multiplayer/PhotonPUN/Scripts/Traits/PunHealthMonitor.cs:97)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <695d1cc93cca45069c528c15c9fdd749>:0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <695d1cc93cca45069c528c15c9fdd749>:0)
Photon.Pun.PhotonNetwork.ExecuteRpc (ExitGames.Client.Photon.Hashtable rpcData, Photon.Realtime.Player sender) (at Assets/Plugins/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:553)
Photon.Pun.PhotonNetwork.RPC (Photon.Pun.PhotonView view, System.String methodName, Photon.Pun.RpcTarget target, Photon.Realtime.Player player, System.Boolean encrypt, System.Object[] parameters) (at Assets/Plugins/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:1266)
Photon.Pun.PhotonNetwork.RPC (Photon.Pun.PhotonView view, System.String methodName, Photon.Pun.RpcTarget target, System.Boolean encrypt, System.Object[] parameters) (at Assets/Plugins/Photon/PhotonUnityNetworking/Code/PhotonNetwork.cs:2940)
Photon.Pun.PhotonView.RPC (System.String methodName, Photon.Pun.RpcTarget target, System.Object[] parameters) (at Assets/Plugins/Photon/PhotonUnityNetworking/Code/PhotonView.cs:604)
Opsive.UltimateCharacterController.AddOns.Multiplayer.PhotonPun.Traits.PunHealthMonitor.OnDamage (System.Single amount, UnityEngine.Vector3 position, UnityEngine.Vector3 direction, System.Single forceMagnitude, System.Int32 frames, System.Single radius, Opsive.UltimateCharacterController.Traits.Damage.IDamageOriginator originator, UnityEngine.Collider hitCollider) (at Assets/Plugins/Opsive/UltimateCharacterController/Add-Ons/Multiplayer/PhotonPUN/Scripts/Traits/PunHealthMonitor.cs:68)
Opsive.UltimateCharacterController.Traits.Health.Damage (Opsive.UltimateCharacterController.Traits.Damage.DamageData damageData) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Traits/Health.cs:312)
Opsive.UltimateCharacterController.Traits.Damage.DamageProcessor.Process (Opsive.UltimateCharacterController.Traits.Damage.IDamageTarget target, Opsive.UltimateCharacterController.Traits.Damage.DamageData damageData) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Traits/Damage/DamageProcessor.cs:170)
Opsive.UltimateCharacterController.Items.Actions.ShootableWeapon.HitscanFire (System.Single strength) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Items/Actions/ShootableWeapon.cs:1001)
Opsive.Shared.Game.SchedulerBase.AddEventInternal[T] (System.Single delay, Opsive.Shared.Game.ScheduledEventBase+InvokeLocation invokeLocation, System.Action`1[T] action, T value) (at <6b6b48297d4f49e9ba244a49e70e6e92>:0)
Opsive.Shared.Game.SchedulerBase.Schedule[T] (System.Single delay, System.Action`1[T] action, T value) (at <6b6b48297d4f49e9ba244a49e70e6e92>:0)
Opsive.UltimateCharacterController.Items.Actions.ShootableWeapon.Fire (System.Single strength) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Items/Actions/ShootableWeapon.cs:783)
Opsive.UltimateCharacterController.Items.Actions.ShootableWeapon.UseItem () (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Items/Actions/ShootableWeapon.cs:691)
Opsive.UltimateCharacterController.Character.Abilities.Items.Use.LateUpdate () (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Character/Abilities/Items/Use.cs:651)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.LateUpdateActiveAbilities (Opsive.UltimateCharacterController.Character.Abilities.Ability[] abilities, System.Int32& abilityCount) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:1057)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.LateUpdateUltimateLocomotion () (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:1043)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.UpdatePositionAndRotation () (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:858)
Opsive.UltimateCharacterController.Character.CharacterLocomotion.UpdatePositionAndRotation (System.Boolean fromAnimatorMove) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Character/CharacterLocomotion.cs:527)
Opsive.UltimateCharacterController.Character.CharacterLocomotion.OnAnimatorMove () (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Character/CharacterLocomotion.cs:1503)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.OnAnimatorMove () (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:1786)

Whenever the player shoots at a bot, the final shot right before the bot dies will not fire, and this error occurs. Then the player can no longer fire a weapon. It seems the stacktrace does mention the ShootableWeapon class, so I'm assuming this problem is part of this error. My OnDamage event looks like this:

C#:
Debug.Log(attacker.name);

if (CharacterState != CharacterState.Incapacitated) CharacterState = CharacterState.Incapacitated;

I am registering it within the Start function like so:

C#:
EventHandler.RegisterEvent<Vector3, Vector3, GameObject>(gameObject, "OnDeath", OnDeath);

I have also tried manually assigning the event directly under the Health component for the prefab, but the same error occurs. Is this some kind of bug, or have I done something wrong here? Any assistance would be greatly appreciated.
 

Justin

Administrator
Staff member
This could be related to the thread below. I will be releasing an update soonish that has this fix. Unfortunately it was a more involved change.

 
This could be related to the thread below. I will be releasing an update soonish that has this fix. Unfortunately it was a more involved change.

Hey Justin, thanks for letting me know, it does look like this problem is related to the thread you mentioned, they have a similar problem with the attacker GameObject being null. Looking forward to an update for this :D
 

suvlimex

New member
Haven't looked into it to be honest, but there will be an update to the PUN add-on in early January that should fix the problem.
I think the problem is in the PunHealthMonitor.cs file, specifically in the OnDamageRPC method.
C#:
 private void OnDamageRPC(float amount, Vector3 position, Vector3 direction, float forceMagnitude, int frames, float radius, int originatorPhotonViewID, uint hitColliderID, int hitItemSlotID)
        {
          
            IDamageOriginator originator = null;
            if (originatorPhotonViewID != -1) {
                var originatorView = PhotonNetwork.GetPhotonView(originatorPhotonViewID);
                if (originatorView != null) {
                 // Sorry, Im wrong, this is the null object. 
                    originator = originatorView.GetComponent<IDamageOriginator>();                 
                }
            }
            var hitCollider = Utility.PunUtility.RetrieveGameObject(m_GameObject, hitColliderID, hitItemSlotID);
            var pooledDamageData = GenericObjectPool.Get<DamageData>();
            pooledDamageData.SetDamage(originator, amount, position, direction, forceMagnitude, frames, radius, hitCollider != null ? hitCollider.GetCachedComponent<Collider>() : null);
            m_Health.OnDamage(pooledDamageData);
            GenericObjectPool.Return(pooledDamageData);
        }

This could be related to the thread below. I will be releasing an update soonish that has this fix. Unfortunately it was a more involved change.


Could it be a clue?

Edit: I'm wrong with line and object.
I'm still looking for a solution, if I find it I will publish it.
 
Last edited:

Paul

New member
Im having the same problem and also think the problem starts in “the PunHealthMonitor.cs file, specifically in the OnDamageRPC method”
 
Version 1.1.16 of the PUN add-on fixes this bug, I have tested it and the attacker GameObject is no longer null when OnDamage and OnDeath are invoked (y)
 

Paul

New member
Did the update went well? First download the installer then install and import. Was my mistake the first time
 

suvlimex

New member
This is what i founded

C#:
        private void OnDamageRPC(float amount, Vector3 position, Vector3 direction, float forceMagnitude, int frames, float radius, int originatorPhotonViewID, uint originatorItemIdentifierID, int originatorSlotID, int originatorItemActionID, uint hitColliderID, int hitItemSlotID)
        {
            IDamageOriginator originator = null;
            if (originatorPhotonViewID != -1) {
                var originatorView = PhotonNetwork.GetPhotonView(originatorPhotonViewID);               
                if (originatorView != null) {
                    originator = originatorView.GetComponent<IDamageOriginator>();                               
                    // If the originator is null then it may have come from an item.
                    if (originator == null) {
                        var itemType = ItemIdentifierTracker.GetItemIdentifier(originatorItemIdentifierID);                       
                        m_Inventory = originatorView.GetComponent<InventoryBase>();

                        if (itemType != null && m_Inventory != null) {
                            var item = m_Inventory.GetItem(itemType, originatorSlotID);
                            // Item is null cause originatorSlotID is -1, why?
                            if (item != null) {
                                originator = item.GetItemAction(originatorItemActionID) as IDamageOriginator;
                            }
                        }
                    }
                }
            }
 

Justin

Administrator
Staff member
Glad that it's working for everyone. @suvlimex - I recommend creating a fresh project just so you can get it working at the base. From there you can see what the changes are within your own project.
 

Paul

New member
Shooting with a weapon for me is working but when I throw a grenade the Attacker is null again.
 

Justin

Administrator
Staff member
I was able to reproduce that - I'll get it fixed in the update going out shortly.
 
Top