I'm trying to make a VR game in Unity and what I'm trying to do is add vr hands that have an animation using an animation controller and blend tree and everything works but the problem is that when I try to use the function .SetActive() but it gives me the error 'Object reference not set to an instance of an object' even though I'm assigning the handModelPrefab to something the error is found in the bottom of the Update() function:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
public class HandPresence : MonoBehaviour
{
public bool showController = false;
public InputDeviceCharacteristics controllerCharacteristics;
public List<GameObject> controllerPrefabs;
public GameObject handModelPrefab;
private InputDevice targetDevice;
private GameObject spawnedController;
private GameObject spawnedHandModel;
private Animator handAnimator;
void Start()
{
List<InputDevice> devices = new List<InputDevice>();
InputDevices.GetDevicesWithCharacteristics(controllerCharacteristics, devices);
if(devices.Count > 0)
{
targetDevice = devices[0];
GameObject prefab = controllerPrefabs.Find(controller => controller.name == targetDevice.name);
if (prefab)
{
spawnedController = Instantiate(prefab, transform);
}
else
{
Debug.LogError("Did not find controller model");
spawnedController = Instantiate(controllerPrefabs[0], transform);
}
spawnedHandModel = Instantiate(handModelPrefab, transform);
handAnimator = spawnedHandModel.GetComponent<Animator>();
}
}
void UpdateHandAnimation()
{
if (targetDevice.TryGetFeatureValue(CommonUsages.trigger, out float triggerValue))
{
handAnimator.SetFloat("Trigger", triggerValue);
}
else
{
handAnimator.SetFloat("Trigger", 0);
}
if (targetDevice.TryGetFeatureValue(CommonUsages.grip, out float gripValue))
{
handAnimator.SetFloat("Grip", gripValue);
}
else
{
handAnimator.SetFloat("Grip", 0);
}
}
void Update()
{
if (showController)
{
spawnedController.SetActive(true);
spawnedHandModel.SetActive(false);
}
else
{
spawnedHandModel.SetActive(true);
spawnedController.SetActive(false);
UpdateHandAnimation();
}
}
}