Solved - Null Exception Error after adding Sword Action

Heya,

Me again. Well, if you are gonna let me use your things, you should expect them to be broken…

After I added the sword action, I noticed (while everything worked) I started to get null exception errors from UnitRagdoll.

I realised this was because we had childed the sword prefab to the unit hand and when comparing “bones”, this was included in the search but not found in the ragdoll. Added it (deactivated) to the ragdoll and lifes a good’un

2 Likes

Yes, this seems to be an oversight in the way the course is written. You can also change the MatchAllChildTransforms function in UnitRagdoll.cs by adding the following null check before recursively calling the function as follows:

    private void MatchAllChildTransforms(Transform root, Transform clone)
    {
        foreach(Transform child in root)
        {
            Transform cloneChild = clone.Find(child.name);

            if(cloneChild != null)
            {
                cloneChild.position = child.position;
                cloneChild.rotation = child.rotation;
            }

            //Add this null check before recursively calling the function
            if(child != null && cloneChild != null)
                MatchAllChildTransforms(child, cloneChild);
        }
    }

This way you don’t have to match all of the transforms in your ragdoll that your original skeleton has.

1 Like

Did you duplicate the Unit prefab to make the Ragdoll or did you drag a brand new visual and start from scratch from there? If you duplicate it like I did then everything should work fine, if you create a brand new visual from scratch then yup you need to test if the child exists.

I ran into this same issue and I did duplicate the unit prefab for the ragdoll. The null reference exception causes destroying of the enemy gameobject to be skipped. I get a spray of ragdolls when either enemy or friendly unit dies by shooting, grenade or sword. The null check suggested above by @Marchris fixed it for me.

I didn’t get any exceptions or errors by following the tutorial.

Ah yea i got the same error. Where basically my ragdoll was not a variant of the unit prefab. But my Enemy Prefab was.

And so modifying the Original Unit prefab, gave the unit and the enemy unit a sword. but not the prefab.

I guess at that point we could just remake the UnitRagdoll to be a variant of the unit. Or handle the null exception.

image

Privacy & Terms