'Unity script not assigning variables

Problem

I have a script that is taking avatar data from an animator in unity to automatically assign bone transforms to classes that will be used for posing; the issue is, even though all the data is there and can be used, the script somehow isn't assigning the data.

The scripts

HandAutoPoser.cs

This is called by editor scripts to grab the finger bone transforms and save them to the script, but it's not doing that expected behavior, even though all the HumanBodyBones are returning a transform, and there are no exceptions being thrown in the console.

public class HandAutoPoser : MonoBehaviour
    {
        private List<HumanBodyBones> _leftFingers = new List<HumanBodyBones>()
        {
            HumanBodyBones.LeftThumbProximal,
            HumanBodyBones.LeftThumbIntermediate,
            HumanBodyBones.LeftThumbDistal,
            HumanBodyBones.LeftIndexProximal,
            HumanBodyBones.LeftIndexIntermediate,
            HumanBodyBones.LeftIndexDistal,
            HumanBodyBones.LeftMiddleProximal,
            HumanBodyBones.LeftMiddleIntermediate,
            HumanBodyBones.LeftMiddleDistal,
            HumanBodyBones.LeftRingProximal,
            HumanBodyBones.LeftRingIntermediate,
            HumanBodyBones.LeftRingDistal,
            HumanBodyBones.LeftLittleProximal,
            HumanBodyBones.LeftLittleIntermediate,
            HumanBodyBones.LeftLittleDistal
        };
        private List<HumanBodyBones> _rightFingers = new List<HumanBodyBones>()
        {
            HumanBodyBones.RightThumbProximal,
            HumanBodyBones.RightThumbIntermediate,
            HumanBodyBones.RightThumbDistal,
            HumanBodyBones.RightIndexProximal,
            HumanBodyBones.RightIndexIntermediate,
            HumanBodyBones.RightIndexDistal,
            HumanBodyBones.RightMiddleProximal,
            HumanBodyBones.RightMiddleIntermediate,
            HumanBodyBones.RightMiddleDistal,
            HumanBodyBones.RightRingProximal,
            HumanBodyBones.RightRingIntermediate,
            HumanBodyBones.RightRingDistal,
            HumanBodyBones.RightLittleProximal,
            HumanBodyBones.RightLittleIntermediate,
            HumanBodyBones.RightLittleDistal
        };
        private List<Transform> _fingers;

        public enum HandSide
        {
            Left,
            Right
        }
        public HandSide Side;

        public HandPoseData ClosedFist;
        public HandPoseData OpenHand;

        [Space]
        public PlayerModelHand Hand = new PlayerModelHand();

        private Animator PlayerModelAnimator => GetComponentInParent<Animator>();

        private void Start()
        {
            // Start is here for when this component is added to a player model during runtime.
            Initialize();
        }

        public void Initialize()
        {
            // Initialize is called by editor scripts while developing.
            Debug.Log("Initialize hand poser.");
            GetFingers();
        }

        private void GetFingers()
        {
            _fingers = new List<Transform>()
            {
                Hand.Thumb.Proximal,
                Hand.Thumb.Intermediate,
                Hand.Thumb.Distal,
                Hand.Index.Proximal,
                Hand.Index.Intermediate,
                Hand.Index.Distal,
                Hand.Middle.Proximal,
                Hand.Middle.Intermediate,
                Hand.Middle.Distal,
                Hand.Ring.Proximal,
                Hand.Ring.Intermediate,
                Hand.Ring.Distal,
                Hand.Little.Proximal,
                Hand.Little.Intermediate,
                Hand.Little.Distal
            };

            switch (Side)
            {
                case HandSide.Left:
                    GetLeftFingers();
                    break;
                case HandSide.Right:
                    GetRightFingers();
                    break;
                default:
                    break;
            }
        }

        private void GetLeftFingers()
        {
            for (int i = 0; i < _fingers.Count; i++)
            {
                Transform finger = PlayerModelAnimator.GetBoneTransform(_leftFingers[i]);
                _fingers[i] = finger;
            }
        }

        private void GetRightFingers()
        {
            for (int i = 0; i < _fingers.Count; i++)
            {
                Transform finger = PlayerModelAnimator.GetBoneTransform(_rightFingers[i]);
                _fingers[i] = finger;
            }
        }
    }

These scripts are used to hold transform data.

PlayerModelHand.cs

[System.Serializable]
    public class PlayerModelHand
    {
        public HandFinger Thumb = new HandFinger();
        public HandFinger Index = new HandFinger();
        public HandFinger Middle = new HandFinger();
        public HandFinger Ring = new HandFinger();
        public HandFinger Little = new HandFinger();
    }

HandFigner.cs

[System.Serializable]
    public class HandFinger
    {
        public Transform Proximal;
        public Transform Intermediate;
        public Transform Distal;
    }


Solution 1:[1]

I think I understand now what I was getting confused with; the _fingers array wasn't containing a reference to the transforms' fields, instead it was just copying the values to a new allocation of data, and when I attempted to write the returned transforms to that array, it was writing to the array it self, not the 'assumed' references I thought the array was going to contain.

Here's the fixed script

public class HandAutoPoser : MonoBehaviour
    {
        private List<HumanBodyBones> _leftBones = new List<HumanBodyBones>()
        {
            HumanBodyBones.LeftThumbProximal,
            HumanBodyBones.LeftThumbIntermediate,
            HumanBodyBones.LeftThumbDistal,
            HumanBodyBones.LeftIndexProximal,
            HumanBodyBones.LeftIndexIntermediate,
            HumanBodyBones.LeftIndexDistal,
            HumanBodyBones.LeftMiddleProximal,
            HumanBodyBones.LeftMiddleIntermediate,
            HumanBodyBones.LeftMiddleDistal,
            HumanBodyBones.LeftRingProximal,
            HumanBodyBones.LeftRingIntermediate,
            HumanBodyBones.LeftRingDistal,
            HumanBodyBones.LeftLittleProximal,
            HumanBodyBones.LeftLittleIntermediate,
            HumanBodyBones.LeftLittleDistal
        };
        private List<HumanBodyBones> _rightBones = new List<HumanBodyBones>()
        {
            HumanBodyBones.RightThumbProximal,
            HumanBodyBones.RightThumbIntermediate,
            HumanBodyBones.RightThumbDistal,
            HumanBodyBones.RightIndexProximal,
            HumanBodyBones.RightIndexIntermediate,
            HumanBodyBones.RightIndexDistal,
            HumanBodyBones.RightMiddleProximal,
            HumanBodyBones.RightMiddleIntermediate,
            HumanBodyBones.RightMiddleDistal,
            HumanBodyBones.RightRingProximal,
            HumanBodyBones.RightRingIntermediate,
            HumanBodyBones.RightRingDistal,
            HumanBodyBones.RightLittleProximal,
            HumanBodyBones.RightLittleIntermediate,
            HumanBodyBones.RightLittleDistal
        };
        

        public enum HandSide
        {
            Left,
            Right
        }
        public HandSide Side;
        public PlayerModelHand Hand = new PlayerModelHand();
        public HandPoseData ClosedFist;
        public HandPoseData OpenHand;

        public void AssignBones()
        {
            Hand.Thumb.Proximal = GetBone(0);
            Hand.Thumb.Intermediate = GetBone(1);
            Hand.Thumb.Distal = GetBone(2);
            Hand.Index.Proximal = GetBone(3);
            Hand.Index.Intermediate = GetBone(4);
            Hand.Index.Distal = GetBone(5);
            Hand.Middle.Proximal = GetBone(6);
            Hand.Middle.Intermediate = GetBone(7);
            Hand.Middle.Distal = GetBone(8);
            Hand.Ring.Proximal = GetBone(9);
            Hand.Ring.Intermediate = GetBone(10);
            Hand.Ring.Distal = GetBone(11);
            Hand.Little.Proximal = GetBone(12);
            Hand.Little.Intermediate = GetBone(13);
            Hand.Little.Distal = GetBone(14);
        }
        private Transform GetBone(int index)
        {
            Animator animator = GetComponentInParent<Animator>();

            switch (Side)
            {
                case HandSide.Left:
                    return animator.GetBoneTransform(_leftBones[index]);
                case HandSide.Right:
                    return animator.GetBoneTransform(_rightBones[index]); ;
                default:
                    break;
            }

            return null;
        }
    }

Moral of the story is, don't assume things will work as simple as you may assume.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 OneSmolCookie