So I'm trying to develop a grab-scale function in my HTC Vive (SteamVR) experience (Unity) where you can grab an Interaction Behaviour object with two hands and use the difference in transforms to adjust the object's localScale. The script I've written in C# using the InteractionBehaviour library, particularly the public member variable interactionBehaviour.isgrabbed, has been persistently giving me an error that the InteractionBehaviour script cannot find an instance of an Interaction Manager.
I only have one Interaction Manager attached to my player, and I know it is set up properly,
as I am able to implement the default Interaction Behaviour script to grab objects and press buttons.
Here's more info about the error:
Location in Hierarchy: jwst_v13(small)handheldall/tower_whole/twr_column
Script producing Error: InteractionBehaviour.cs
Error Type: NullReferenceException: Object reference not set to an instance of an object
Script I think is Causing Error: multi_grasp_scale.cs (my own script pasted below)
Location of Error on Script: Lines 36 and 63 (when I call the member functions of InteractionBehaviour and InteractionController objects)
Peripheral (possibly relevant) errors: LeapVRTemporalWarping.cs
Peripheral Error Type: NullReferenceException: Object reference not set to an instance of an object
Possibly Useful Information: My Interaction Manager is sitting under a VRTK (popular virtual reality SDK) Player object. This runs an automatic setup switcher which enables SteamVR (my main camera) on runtime. I haven't been able to tell yet whether or not this is producing the error because the game begins with SteamVR set as inactive.
I've pasted a screenshot of my error and my code below. I'd appreciate any help I can get!
//multi_grasp_scale.cs
using Leap.Unity.Attributes;
using Leap.Unity.Query;
using Leap.Unity.Interaction.Internal;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using Leap.Unity.Space;
using InteractionEngineUtility;
using Leap.Unity.RuntimeGizmos;
namespace Leap.Unity.Interaction
{
public class multi_grasp_scale : MonoBehaviour
{
public IInteractionBehaviour interactionBehaviour;
public InteractionController rightHand;
public InteractionController leftHand;
public Transform hand_left;
public Transform hand_right;
// Use this for initialization
void Start()
{
float start_scale = this.transform.localScale.x;
}
// Update is called once per frame
void Update()
{
if (interactionBehaviour.isGrasped)
{
Debug.LogFormat(this.transform.gameObject, "object grasped");
Vector3 start_scale = this.transform.localScale;
if (leftHand.graspedObject.name == rightHand.graspedObject.name) // ERROR
{
Debug.LogFormat(this.transform.gameObject, "object double grasped");
//stop all coroutines
StopAllCoroutines();
//call scale_grab())
StartCoroutine(scale_grab());
//prevent coroutine from being called more than once
//hopOut = true;
}
}
//hopOut = false;
}
IEnumerator scale_grab()
{
Debug.LogFormat(this.transform.gameObject, "scale_grab() coroutine called");
//grab start distance between hands and generate scalar value (pythagorean formula)
Vector3 start_dist_vect = hand_left.position - hand_right.position;
string vect_debug = start_dist_vect.ToString();
Debug.Log(vect_debug);
float start_dist_val = (float)Math.Sqrt(Math.Pow((float)start_dist_vect.x, 2f) + Math.Pow((float)start_dist_vect.y, 2f) + Math.Pow((float)start_dist_vect.z, 2f));
Debug.LogFormat("start_dist_val = {0}", start_dist_val);
// while still grabbed by two hands, perform scaling
while (leftHand.graspedObject.name == rightHand.graspedObject.name) // ERROR
{
// --------------------- by scale factor ----------------------
Debug.LogFormat(this.transform.gameObject, "scale_grab() while executing");
//grab transient distance between hands and gen scalar value
Vector3 trans_dist_vect = hand_left.position - hand_right.position;
float trans_dist_val = (float)Math.Sqrt(Math.Pow((float)trans_dist_vect.x, 2f) + Math.Pow((float)trans_dist_vect.y, 2f) + Math.Pow((float)trans_dist_vect.z, 2f));
//string trans_vect_debug = trans_dist_vect.ToString();
//Debug.Log(trans_vect_debug);
// find scale by dividing trans/start distance vals
float scale_val = trans_dist_val / start_dist_val;
Debug.LogFormat("scale_val = {0}", scale_val);
// transform the local scale by a factor of scale_val
this.transform.localScale *= scale_val;
// ---------------------- by direct vector values -----------------
//this.transform.localScale = trans_dist_vect;
yield return null;
}
// once let go, allow coroutine to be called again
//hopOut = false;
//Debug.LogFormat("hopOut = {b}", hopOut);
}
}
}