[英]C# OnTriggerEnter NullReferenceException
我在學習中使用Zigfu的ZDK for Game Project。 我拿了標准化身並將兩個碰撞器(球體碰撞器與剛體)連接到手上。 一個用於左手,一個用於右手。 我在頭像周圍建造了一些(24)盒子對撞機,以跟蹤手是否正在進入一個新區域。 每個框都有一個“id”,從0到23以下腳本:
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中處理數組,但沒有任何效果。 我不明白碰撞和陣列之間的聯系?!
您的數組尚未初始化。 你得到一個NullReferenceException
因為你的數組是null的引用,因為它沒有被分配。
例如,您可以在Awake
初始化它們:
int[] array1 = new int[5];
public static bool[] handsLeft;
// Use this for initialization
void Awake () {
activity = 0.0f;
handsLeft = new bool[ARRAY_SIZE];
}
也許您已嘗試從檢查器初始化這些數組字段,但它們被聲明為靜態,因此當您從編輯器切換到播放模式時, Unity3D
不會將它們序列化 。
注意所有playerCommunication
類的實例都會共享一個靜態字段。 因此,如果您希望讓幾個GameObject使用此腳本,並且在數組中使用不同的值,則不應將它們聲明為靜態。
如果你想有效地將它們聲明為靜態,因為你無法知道類的創建順序,最好在類static static中初始化它們:
static playerCommunication ()
{
handsLeft = new bool[ARRAY_SIZE];
handsRight = new bool[ARRAY_SIZE];
}
獲得nullpointerexception的原因是因為您嘗試在數組中添加尚未初始化的對象。
您有以下聲明:
public int id;
之后,您有以下代碼行:
print("leftHand entered: " + id);
將其更改為,例如:
Debug.Log("ID value is now: " + id);
您可能會看到以下內容:
ID value is now: null
你尚未給id
一個值。 當你調用playerCommunication.handsLeft[id] = true;
,您實際上是在嘗試將數組的null'est值設置為true
。 確保將id
value設置為某個值。 你說每個盒子的id值都是0到23.你是否在Unity屬性視圖中正確設置了?
我猜你得到異常的原因是因為你沒有訪問播放器上的playerCommunication腳本,而是試圖使用腳本文件夾中的那個,我想這里沒有任何設置在公眾手上等
你需要做的是替換這樣的行:
playerCommunication.handsLeft[id] = true;
有了這個:
GameObject.Find("Avatar").GetComponent<playerCommunication>().handsLeft[id] = true;
其中“Avatar”將是頭像/玩家游戲對象的名稱,而playerCommunication是該對象中具有handsLeft公共bool的腳本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.