- Home /
C# OnTriggerEnter NullReferenceException
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?!
Answer by Graham-Dunnett · Apr 23, 2013 at 12:32 PM
The code you have shown tells us that you have an array called handsRight[]
. However nowhere do you create this array. You have to create built-in, .net arrays before using them, see the Array class in the script reference. (The docs are down at the moment, otherwise I'd send you a link.)
Initializing the arrays in the awake() function solved my problem, thanks!
@dome_b: I've converted your answer into a comment. Answers should answer the question.
Usually you always have to create an array before you can use it, but Unity might be a bit confusing because public member variables which are serialized by Unity are initialized automatically / by the inspector.
ps: for further "comments" use the "add new comment" button. Also don't forget to vote helpful answers and accept an answer if it solved your question.
Your answer
![](https://koobas.hobune.stream/wayback/20220613100849im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Colliding two GameObjects 1 Answer
On Trigger Enter, Collide with object, specific collision 1 Answer
Trigger Spawning? 1 Answer
Keep doing something while colliding 1 Answer
Slow collision triggers late 0 Answers