我在学习中使用 Zigfu 的 ZDK 进行游戏项目。我拿了标准的化身并在手上附加了两个对撞机(带刚体的球体对撞机)。一个用于左手,一个用于右手。我在化身周围构建了一些 (24) 盒式对撞机,以跟踪手是否正在进入新区域。每个盒子都有一个从 0 到 23 的“id”以及以下脚本:
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; //here ist the exception
}
else if (other.tag == "handsRightTracking"){
print("rightHand entered: " + id);
playerCommunication.activity += 0.1f;
playerCommunication.handsRight[id] = true; //here ist the exception
}
}
void OnTriggerExit (Collider other) {
if(other.tag == "handsLeftTracking"){
print("leftHand exited: " + id);
playerCommunication.activity += 0.01f;
playerCommunication.handsLeft[id] = false; //here ist the exception
}
else if (other.tag == "handsRightTracking"){
print("rightHand exited: " + id);
playerCommunication.activity += 0.01f;
playerCommunication.handsRight[id] = false; //here ist the exception
}
}
}
在播放器的另一个脚本中,我想使用这些碰撞。handsTracking.cs 应该只编辑 playerCommunication.cs 脚本中的值:
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);
}
}
到目前为止,这工作正常。但我得到以下例外:
NullReferenceException: Object reference not set to an instance of an object
handsTracking.OnTriggerEnter (UnityEngine.Collider other) (at Assets/SeriousGame/Scripts/handsTracking.cs:19)
如果我推荐我尝试编辑数组的行,错误就消失了。我试图在 playerCommunication 脚本中调用一个函数,或处理 handsTracking.cs 中的数组,但没有任何效果。我不明白碰撞和数组之间的联系?!