Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by dome_b · Apr 23, 2013 at 12:19 PM · collisiontriggerontriggerenterexception

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?!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dome_b · May 02, 2013 at 03:12 PM 0
Share

Initializing the arrays in the awake() function solved my problem, thanks!

avatar image Bunny83 · May 02, 2013 at 03:17 PM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges