-2

So, I have been trying to make inverse kinematics and while I did I came up with this code to get info on the transform and length of individual bones in a chain. It calculates the needed data and puts it in a table of "BoneSpecialStruct" Structures "BoneChain". Then it's supposed to write the contents of "BoneChain" in the debug console.

Actual IK is WIP.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InverseKinematics : MonoBehaviour
{

BoneSpecialStruct HighestParent;
BoneSpecialStruct[] BoneChain;
public int TargetChainLength;
public Transform IKtarget;

public struct BoneSpecialStruct
{
    public Transform transform;
    public float length;
    public BoneSpecialStruct(Transform transform, float length)
    {
        this.transform = transform;
        this.length = length;
    }
}

void Start()
{
    HighestParent.transform = transform;
    Vector3 TempLength = new Vector3();
    TempLength.x = HighestParent.transform.parent.position.x - HighestParent.transform.position.x;
    TempLength.y = HighestParent.transform.parent.position.y - HighestParent.transform.position.y;
    TempLength.z = HighestParent.transform.parent.position.z - HighestParent.transform.position.z;
    HighestParent.length = Mathf.Pow(TempLength.y, 2) + (Mathf.Pow(TempLength.x, 2) + Mathf.Pow(TempLength.z, 2));
    BoneChain[0] = new BoneSpecialStruct(HighestParent.transform, HighestParent.length);
    for (int i = 0; i < TargetChainLength; i++)
    {
        HighestParent.transform = HighestParent.transform.parent;
        TempLength.x = HighestParent.transform.parent.position.x - HighestParent.transform.position.x;
        TempLength.y = HighestParent.transform.parent.position.y - HighestParent.transform.position.y;
        TempLength.z = HighestParent.transform.parent.position.z - HighestParent.transform.position.z;
        HighestParent.length = Mathf.Pow(TempLength.y, 2) + (Mathf.Pow(TempLength.x, 2) + Mathf.Pow(TempLength.z, 2));
        BoneChain[i] = new BoneSpecialStruct(HighestParent.transform, HighestParent.length);
    }
    foreach (BoneSpecialStruct Bone in BoneChain)
    {
        Debug.Log(Bone);
    }
}

void FixedUpdate()
{
    //WIP
}
}

Expected to get info on bones in a chain however, when it is run I get error

NullReferenceException: Object reference not set to an instance of an object
InverseKinematics.Start () (at Assets/InverseKinematics.cs:32)

Don't how to fix it. Help.

I am using unity 2020.4f1 Personal, with Visual Studio Code and .NET 5.0

1 Answers1

0

It looks like you're trying to use an array (BoneChain) without initialising it. You therefore get a NullReferenceException when you try to assign to its first element using BoneChain[0] = ....

You need to initialise the array before assigning to its elements, using something like:

BoneChain = new BoneSpecialStruct[TargetChainLength];

at the start of the Start method.

Alternatively you could use (say) a List<BoneSpecialStruct>, add the elements in turn and then call .ToArray() at the end, before your foreach loop.

Neil T
  • 1,794
  • 1
  • 12
  • 21