Iam trying to get Vr Hand models using xr integration toolkit. iam using a hand controller script where it get input from controllers using and using another script which is on hand models to animate them, but iam getting a null reference exception error.
This is connected to the controller.
[RequireComponent(typeof(ActionBasedController))]
public class HandController : MonoBehaviour
{
// Start is called before the first frame update
ActionBasedController controller;
public Hand hand;
void Start()
{
controller = GetComponent();
}
// Update is called once per frame
void Update()
{
hand.SetGrip(controller.selectAction.action.ReadValue());
hand.SetTrigger(controller.selectAction.action.ReadValue());
}
}
this is the hand script to animate
public class Hand : MonoBehaviour
{
Animator animator;
private float gripTarget;
private float triggerTarget;
private float gripCurrent;
private float triggerCurrent;
public float speed;
// Start is called before the first frame update
void Start()
{
animator = GetComponent();
}
// Update is called once per frame
void Update()
{
UpdateHandAnimation();
}
internal void SetGrip(float v)
{
gripTarget = v;
}
internal void SetTrigger(float v)
{
triggerTarget = v;
}
Can you please copy the exception message from the Console tab so we can see the stack trace and attach a screenshot of your hierarchy for the Hand GameObject and the HandController GameObject so that we are able to verify the setup? If possible, you can attach your project instead so we can take a look.
Some things that could be causing the issue since I don’t know which line was causing the NullReferenceException:
- Make sure the Select Action is set on your ActionBasedController behavior. If you have Use Reference enabled but do not have a reference set to an Input Action in an external asset, that
action
property could be null.
- Make sure the ActionBasedController behavior is on the same GameObject as your HandController. If you added the RequireComponent attribute later, it does not automatically add the component at runtime, it will only automatically add it when the HandController is initially added.
- The Animator component may not be on the same GameObject as the Hand component, so it could be throwing in your UpdateHandAnimation method.
- You can also read the values from the ActionBasedController.selectInteractionState instead of reading from the InputAction since the ActionBasedController already reads the float value each frame, and it could be more efficient to read it from that struct instead of from the InputAction again.