Attacker GameObject is null when Die func is raised

prashant

New member
1681297111949.png
Reference - PunHealthMonitor.cs

  1. This Script is attached to the Character. When Character is Dying OnDie Function is Called But Attacker is null so that else Condition executed . As you can see in the Pic
    image.png



    image.png


    var attacker = damageData.DamageSource?.SourceOwner;
    in this attacker is null when player is dying .... why??

    image.png


    image.png

    and this only give null when Gameobject is Player if it is not Player than its working fine.

    I am Stuck on this problem from 10 days...


 
Thanks for the details. I was able to reproduce this and will fix it for the next update. I'll let you know as soon as I have the fix.
 
I do it via damage for the asset I am working on but this can be done the same with on death. I only ever run the check on master, so it got a little tricky. Here you go for reference... This is messy but works flawlessly and can also give you the attacking weapon.


C#:
    // if we get here then target was a player that got killed, so
            // see if we know about the damage source

            Transform sourceTransform = null;
            GameObject attackingObject = null;
            IDamageSource source = damageData.DamageSource;
            var ctx = damageData.ImpactContext;
            float damage = damageData.Amount;

            if (source != null)
            {
                attackingObject = source.SourceGameObject;

                // If the originator is an item then more data needs to be sent.
                if (source is CharacterItemAction)
                {
                    var itemAction = source as CharacterItemAction;
                    sourceTransform = itemAction.CharacterTransform;
                    if (attackingObject = null)
                        attackingObject = itemAction.GameObject;
                    //TODO: Add stats
                }
                if (sourceTransform == null && source.TryGetCharacterLocomotion(out CharacterLocomotion characterLocomotion))
                    sourceTransform = characterLocomotion.transform;

                //Debug.Log("GOT FROM SOURCE: " + (sourceTransform ? sourceTransform : "NULL"));
                //MPDebug.Log("GOT FROM SOURCE: " + (sourceTransform ? sourceTransform : "NULL"));
            }
            if (sourceTransform == null || attackingObject == null && ctx.ImpactCollisionData != null)
            {
                if (attackingObject && ctx.CharacterItemAction != null)
                {
                    if (attackingObject == null)
                        attackingObject = ctx.CharacterItemAction.GameObject;
                    //TODO: Add stats
                }

                if (sourceTransform == null && ctx.ImpactCollisionData.SourceCharacterLocomotion)
                {
                    sourceTransform = ctx.ImpactCollisionData.SourceCharacterLocomotion.transform;
                }
                if (sourceTransform == null || attackingObject == null && ctx.ImpactCollisionData.SourceGameObject != null)
                {
                    if (sourceTransform == null)
                        sourceTransform = ctx.ImpactCollisionData.SourceGameObject.transform;
                    if (attackingObject == null)
                        attackingObject = ctx.ImpactCollisionData.SourceGameObject;
                }

                //    Debug.Log("GOT ALTERNATIVELY: " + (sourceTransform? sourceTransform:"NULL"));
                //    MPDebug.Log("GOT ALTERNATIVELY: " + (sourceTransform ? sourceTransform : "NULL"));
            }
This also drives the kill feed with some other code that follows it. A polished version of kill feeds etc will all be in my asset when released ;)
 
Last edited:
Thanks for the details. I was able to reproduce this and will fix it for the next update. I'll let you know as soon as I have the fix
I do it via damage for the asset I am working on but this can be done the same with on death. I only ever run the check on master, so it got a little tricky. Here you go for reference... This is messy but works flawlessly and can also give you the attacking weapon.


C#:
    // if we get here then target was a player that got killed, so
            // see if we know about the damage source

            Transform sourceTransform = null;
            GameObject attackingObject = null;
            IDamageSource source = damageData.DamageSource;
            var ctx = damageData.ImpactContext;
            float damage = damageData.Amount;

            if (source != null)
            {
                attackingObject = source.SourceGameObject;

                // If the originator is an item then more data needs to be sent.
                if (source is CharacterItemAction)
                {
                    var itemAction = source as CharacterItemAction;
                    sourceTransform = itemAction.CharacterTransform;
                    if (attackingObject = null)
                        attackingObject = itemAction.GameObject;
                    //TODO: Add stats
                }
                if (sourceTransform == null && source.TryGetCharacterLocomotion(out CharacterLocomotion characterLocomotion))
                    sourceTransform = characterLocomotion.transform;

                //Debug.Log("GOT FROM SOURCE: " + (sourceTransform ? sourceTransform : "NULL"));
                //MPDebug.Log("GOT FROM SOURCE: " + (sourceTransform ? sourceTransform : "NULL"));
            }
            if (sourceTransform == null || attackingObject == null && ctx.ImpactCollisionData != null)
            {
                if (attackingObject && ctx.CharacterItemAction != null)
                {
                    if (attackingObject == null)
                        attackingObject = ctx.CharacterItemAction.GameObject;
                    //TODO: Add stats
                }

                if (sourceTransform == null && ctx.ImpactCollisionData.SourceCharacterLocomotion)
                {
                    sourceTransform = ctx.ImpactCollisionData.SourceCharacterLocomotion.transform;
                }
                if (sourceTransform == null || attackingObject == null && ctx.ImpactCollisionData.SourceGameObject != null)
                {
                    if (sourceTransform == null)
                        sourceTransform = ctx.ImpactCollisionData.SourceGameObject.transform;
                    if (attackingObject == null)
                        attackingObject = ctx.ImpactCollisionData.SourceGameObject;
                }

                //    Debug.Log("GOT ALTERNATIVELY: " + (sourceTransform? sourceTransform:"NULL"));
                //    MPDebug.Log("GOT ALTERNATIVELY: " + (sourceTransform ? sourceTransform : "NULL"));
            }
This also drives the kill feed with some other code that follows it. A polished version of kill feeds etc will all be in my asset when released ;)
is this separate script? how can I use in my code?
 
You can use this in your own assembly or modify health, I never recommend altering UCC core though as it makes for more work when updating, your better off creating an extension or writing your own variant.
 
I do not have an ETA for the next update but you can fix this by adding the following to line 212 of SimpleDamage:

Code:
                    pooledDamageData.DamageSource = impactData.SourceComponent as IDamageSource;
 
1682748444007.png

I am using all guns but mainly using assault rifle and above scripts OnDeath run else condition
Please Provide me Solution asap so I can continue my work.

Thank you.
 
Are you able to tell me step by step how to reproduce it in the PUN add-on demo scene? I just tried the assault rifle and the attacker was correctly sent.
 
Top