Hey guys, I hate to post this here, but I haven’t been able to find my problem all day. I built a game to use the RazerHydra to use basic functions. I am getting the following errors: NullReferenceException: Object reference not set to an instance of an object Move.Update()(at Assets/Move.cs:54). And the error NullReferenceException: Object reference not set to an instance of an object Move.Update()(at Assets/Grabber.cs:27). They both point to these lines: transform.Translate(hydra.analogs[0]); and bool grabber = hydra.buttons[9] == 1; This code was working perfect until I began building my project when I noticed I had some errors. I thought it was from the Razer Hydra itself, but apparently not.
Here’s my code:
Move.cs
using UnityEngine;
using System.Collections;
public class Move : MonoBehaviour
{
public float moveSensitivity = .3F;
public float lookSensitivity = .3F;
///hold/drop item code///
public GameObject block;
public Transform node1;
public RazerHydra hydra;
//CreateCube creator;
void Spawn(){
bool currentlyDown = hydra.buttons[1]==1;
bool buttonDown = false;
if(currentlyDown && !buttonDown)
{
buttonDown = true;
Instantiate(block,node1.transform.position,node1.transform.rotation);
}
if(!currentlyDown)
{
buttonDown=false;
}
}
// Use this for initialization
void Start ()
{
hydra = GetComponent<RazerHydra>();
print("created hydra");
}
void Update ()
{
//Analog sticks for both controllers perform same function
transform.Translate(hydra.analogs[0]);
//Left controller controls rotation/facing
transform.rotation = hydra.orientations[1];
float moveBack = 15 * Time.smoothDeltaTime * hydra.buttons[13];
transform.Translate (-Vector3.forward * moveBack);
Spawn ();//call instantiate function
}
}
Grabber.cs
using UnityEngine;
using System.Collections;
public class Grabber : Move {
//RaycastHit hitter;
RaycastHit hit;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Rigidbody body = hit.rigidbody;
if(Physics.Raycast(transform.position, transform.forward, out hit, 10.0F))
{
print("A " + hit.transform.name + " object is in Raycast view");
}
else print("Nothing.");
bool grabber = hydra.buttons[9] == 1;
// bool dropper = hydra.buttons[9] == 0;
if (hit.rigidbody && grabber)
{
body.transform.parent = transform;
body.isKinematic = true;
}
if (hit.rigidbody && !grabber){
body.transform.parent = null;
body.isKinematic = false;
}
bool bigger = hydra.buttons[2] == 1;
bool smaller = hydra.buttons[3] == 1;
if (hit.rigidbody && bigger)
{
body.transform.localScale = Vector3.one * 10.0f;
}
if (hit.rigidbody && smaller)
{
body.transform.localScale = Vector3.one * .5f;
}
bool rotate = hydra.buttons[4] == 1;
if (hit.rigidbody && rotate)
{
body.transform.Rotate (Vector3.forward * Time.deltaTime * 100);
}
bool delete = hydra.buttons[11] == 1;
if (hit.rigidbody && delete)
{
Destroy(body);
}
}
}
RazerHydra.cs
using UnityEngine;
using System.Collections;
public class RazerHydra : MonoBehaviour {
public enum Buttons {
LSTART=0,L1=1,L2=2,L3=3,L4=4,LT=5,LTHUMB=7,
RSTART=8,R1=9,R2=10,R3=11,R4=12,RT=13,RTHUMB=15
}
public string vrpnName = "Hydra0@localhost:3883";
public Vector3[] positions = new Vector3[2];
public Quaternion[] orientations = new Quaternion[2];
public int[] buttons = new int[16];
public Vector3[] analogs = new Vector3[2];
private float[] pos = new float[3];
private float [] ori = new float[4];
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float[] analogData = new float[6];
UnityVRPN.getAnalogInfo(vrpnName,6,analogData);
analogs[0].Set(analogData[0],analogData[1],analogData[2]);
analogs[1].Set(analogData[3],analogData[4],analogData[5]);
UnityVRPN.getButtonInfo(vrpnName,16,buttons);
for(int i=0;i<2;i++){
UnityVRPN.getTrackerInfo(vrpnName,i,pos,ori);
positions*.Set(pos[0],-pos[2],-pos[1]);*
-
//force values to stay in -z hemisphere*
_ if(positions*.z > 0){_
positions _= -1positions*;
}
orientations.Set(ori[3],ori[1],-ori[2],ori[0]);
}*_
* //Debug.Log(“”+analogs[0].x+“,”+analogs[0].y+“,”+analogs[0].z);*
* }*
* void OnGUI(){*
* //GUI.Label(new Rect(0,0,500,500),“”+analogs[0].x+“,”+analogs[0].y+“,”+analogs[0].z);*
* }*
}