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
0
Question by cameronmx9 · Feb 13, 2013 at 11:52 PM · errornullobject reference

NullReferenceException: Object reference not set to an instance of an object

Hey guys, I hate to post this here, but I haven't been able to find my problem all day. I built a game to use the RazerHydra to use basic functions. I am getting the following errors: NullReferenceException: Object reference not set to an instance of an object Move.Update()(at Assets/Move.cs:54). And the error NullReferenceException: Object reference not set to an instance of an object Move.Update()(at Assets/Grabber.cs:27). They both point to these lines: transform.Translate(hydra.analogs[0]); and bool grabber = hydra.buttons[9] == 1; This code was working perfect until I began building my project when I noticed I had some errors. I thought it was from the Razer Hydra itself, but apparently not.

Here's my code:

Move.cs

 using UnityEngine;
 using System.Collections;
 public class Move : MonoBehaviour
 {
 
 public float moveSensitivity = .3F;
 public float lookSensitivity = .3F;
     ///hold/drop item code/// 
 
     
 
 public GameObject block;
 public Transform node1;
 
 
 public RazerHydra hydra;
 //CreateCube creator;
 
 
 void Spawn(){
         
         bool currentlyDown = hydra.buttons[1]==1;
         bool buttonDown = false;
 
         if(currentlyDown && !buttonDown)
             {
                 buttonDown = true;
                 Instantiate(block,node1.transform.position,node1.transform.rotation);
             }
         if(!currentlyDown)
             {
                 buttonDown=false;
             }
 }
     
 
 // Use this for initialization
 void Start ()
 {
 hydra = GetComponent<RazerHydra>();
         print("created hydra");
 
 
 
 
 }
 
     
 
     
 
 void Update ()
 {
         
 //Analog sticks for both controllers perform same function
 transform.Translate(hydra.analogs[0]);
 
 //Left controller controls rotation/facing
 transform.rotation = hydra.orientations[1];
         
 float moveBack = 15 * Time.smoothDeltaTime * hydra.buttons[13];    
 transform.Translate (-Vector3.forward * moveBack);
       
 Spawn ();//call instantiate function
 
 }
 }

Grabber.cs

 using UnityEngine;
 using System.Collections;
 
 public class Grabber : Move {
     
     //RaycastHit hitter;
     
     RaycastHit hit;
         
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         
         Rigidbody body = hit.rigidbody;
         
         if(Physics.Raycast(transform.position, transform.forward, out hit, 10.0F))
         {
             print("A " + hit.transform.name + " object is in Raycast view");
         }
         else print("Nothing.");
         
         bool grabber = hydra.buttons[9] == 1;
     //    bool dropper = hydra.buttons[9] == 0;
         
         
         
         
             if (hit.rigidbody && grabber)
             {
                 body.transform.parent = transform;
                 body.isKinematic = true;
             
             }
             if (hit.rigidbody && !grabber){
                 body.transform.parent = null;
                 body.isKinematic = false;
             }
         
         bool bigger = hydra.buttons[2] == 1;
         bool smaller = hydra.buttons[3] == 1;
         
         if (hit.rigidbody && bigger)
         {
             body.transform.localScale = Vector3.one * 10.0f;
         }
         
         if (hit.rigidbody && smaller)
         {
             body.transform.localScale = Vector3.one * .5f;    
         }
         
         bool rotate = hydra.buttons[4] == 1;
         
         if (hit.rigidbody && rotate)
         {
             body.transform.Rotate (Vector3.forward * Time.deltaTime * 100);    
         }
         
         bool delete = hydra.buttons[11] == 1;
         
         if (hit.rigidbody && delete)
         {
         Destroy(body);    
         }
         
         
         
 
         
 
         }
 }


RazerHydra.cs

 using UnityEngine;
 using System.Collections;
 
 public class RazerHydra : MonoBehaviour {
     public enum Buttons {
         LSTART=0,L1=1,L2=2,L3=3,L4=4,LT=5,LTHUMB=7,
         RSTART=8,R1=9,R2=10,R3=11,R4=12,RT=13,RTHUMB=15
     }
     public string vrpnName = "Hydra0@localhost:3883";
     public Vector3[] positions = new Vector3[2];
     public Quaternion[] orientations = new Quaternion[2];
     public int[] buttons = new int[16];
     public Vector3[] analogs = new Vector3[2];
     
     private     float[] pos = new float[3];
     private     float [] ori = new float[4];
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         float[] analogData = new float[6];
         UnityVRPN.getAnalogInfo(vrpnName,6,analogData);
         analogs[0].Set(analogData[0],analogData[1],analogData[2]);
         analogs[1].Set(analogData[3],analogData[4],analogData[5]);
         
         UnityVRPN.getButtonInfo(vrpnName,16,buttons);
 
         for(int i=0;i<2;i++){
             UnityVRPN.getTrackerInfo(vrpnName,i,pos,ori);
             positions[i].Set(pos[0],-pos[2],-pos[1]);
             //force values to stay in -z hemisphere
             if(positions[i].z > 0){
                 positions[i] = -1*positions[i];
             }
             orientations[i].Set(ori[3],ori[1],-ori[2],ori[0]);
         }
         
         
         //Debug.Log(""+analogs[0].x+","+analogs[0].y+","+analogs[0].z);
     }
     
     void OnGUI(){
         //GUI.Label(new Rect(0,0,500,500),""+analogs[0].x+","+analogs[0].y+","+analogs[0].z);
     }
 }
 
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 vbbartlett · Feb 14, 2013 at 04:17 PM

My initial guess is because you have an warning in the script compilation stating that Start has an inheritances problem. Since Grabber is derived from Move, when it calls Start it only calls the Grabber Start() which doesn't call the base class start. You need to use the virtual and override keywords

 // Start for Move
 public virtual Start()
 
 //Start for Grabber
 public override Start()
 {
    base.Start();
    // check to verify that you won't run into error later.
    if (hydra == null)
        Debug.LogError("Error RazerHydra component not found");
 }
Comment
Add comment · 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

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Null reference exception error in Unity, Please Help! 1 Answer

Error CS1519 Help 1 Answer

Object Reference Error On Camera Script 1 Answer

CAN SOMEONE FINALLY HELP ME! 3 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