Hello,
I’m using the ZDK from Zigfu for Game Project on my study. I took the standard avatar and attached two colliders (sphere colliders with rigid body) to the hands. One for the left hand, and one for the right hand.
I build some (24) box colliders around the avatar, to track if the hands are entering a new area. Every box has an “id” from 0 to 23 an the following script:
using UnityEngine;
using System.Collections;
public class handsTracking : MonoBehaviour {
public int id;
void OnTriggerEnter (Collider other) {
if(other.tag == "handsLeftTracking"){
print("leftHand entered: " + id);
playerCommunication.activity += 0.1f;
playerCommunication.handsLeft[id] = true;
}
else if (other.tag == "handsRightTracking"){
print("rightHand entered: " + id);
playerCommunication.activity += 0.1f;
playerCommunication.handsRight[id] = true;
}
}
void OnTriggerExit (Collider other) {
if(other.tag == "handsLeftTracking"){
print("leftHand exited: " + id);
playerCommunication.activity += 0.01f;
playerCommunication.handsLeft[id] = false;
}
else if (other.tag == "handsRightTracking"){
print("rightHand exited: " + id);
playerCommunication.activity += 0.01f;
playerCommunication.handsRight[id] = false;
}
}
}
In another script on the player I want to use these collisions. The handsTracking.cs should only edit the values in the playerCommunication.cs script:
using UnityEngine;
using System.Collections;
public class playerCommunication : MonoBehaviour {
public static bool[] handsRight;
public static bool[] handsLeft;
public static float activity;
public float fallback;
// Use this for initialization
void Awake () {
activity = 0.0f;
}
// Update is called once per frame
void Update () {
if((activity - fallback * Time.deltaTime) >= 0.0f){
activity -= fallback * Time.deltaTime;
}
}
void OnGUI () {
GUI.Box (new Rect (10,200,150,20), "Activity: " + activity);
}
}
This works fine, so far. But I’m getting the following exceptions:
NullReferenceException: Object reference not set to an instance of an object
handsTracking.OnTriggerEnter (UnityEngine.Collider other) (at Assets/SeriousGame/Scripts/handsTracking.cs:19)
If I commend out lines where I tried to edit the array, the errors are gone.
I tried to call a function in the playerCommunication script, or to handle the arrays in the handsTracking.cs , but nothing works. I don’t understand the link between the collision and the array?!