Search Unity

How to fix GvrHead.cs and GvrEye.cs script to prevent it to crash AR/VR app' ?

Discussion in 'Vuforia' started by Spasher, Sep 27, 2017.

  1. Spasher

    Spasher

    Joined:
    Dec 30, 2014
    Posts:
    11
    Hi !

    I develop a VR and AR app with the Vuforia (6.2.10) and GoogleVR (V1.0.1) packages with the 5.4.1f1 Unity version.

    To be compatible with the Vuforia package (Augmented reality), the GvrHead.cs and GvrEye.cs is modified : How to Integrating Cardboard to the AR/VR Sample

    When I start my scene, in my console appears these error messages :

    001.jpg

    The problem is that i have added the following codes in each scripts :

    GvrHead.cs modified to be compatible with the vuforia package
    Code (CSharp):
    1.     if (OnHeadUpdated != null) {
    2.       OnHeadUpdated(gameObject);
    3.     }
    4.         // The part of the code added to the base script
    5.         Vuforia.VuforiaBehaviour.Instance.UpdateState (false, true);

    GvrEye.cs modified to be compatible with the vuforia package
    Code (CSharp):
    1.   public void UpdateStereoValues() {
    2.     Matrix4x4 proj = GvrViewer.Instance.Projection(eye);
    3.     realProj = GvrViewer.Instance.Projection(eye, GvrViewer.Distortion.Undistorted);
    4.  
    5.     [...]
    6.  
    7.         float windowAspect = (float)Screen.width / Screen.height;
    8.         cam.rect = GvrCameraUtils.FixEditorViewport(cam.rect, profileAspect, windowAspect);
    9.       }
    10.     }
    11.         // The part of the code added to the base script
    12.         Vuforia.VuforiaBehaviour.Instance.ApplyCorrectedProjectionMatrix(proj, eye == GvrViewer.Eye.Left);
    13.   }
    I don't quite understand what "NullReferenceException : Object reference not set to an instance of an object" means and how can I fix it ?

    Thanks !
     
  2. Vuforia-Strasza

    Vuforia-Strasza

    Official Vuforia Employee Vuforia

    Joined:
    Jun 13, 2017
    Posts:
    548
    Hello Spasher,

    It looks like you placed the wrong line of code from the integration guide. In the GvrEye.cs file, replace the line you added with the following line:
    Code (CSharp):
    1. Vuforia.VuforiaARController.Instance.ApplyCorrectedProjectionMatrix(proj, eye == GvrViewer.Eye.Left); // Vuforia 6.2
    Thanks!
     
  3. Spasher

    Spasher

    Joined:
    Dec 30, 2014
    Posts:
    11
    Hi Strasza,

    I tried to modify the code according to your advice, and this is what the console shows me :

    001.jpg

    In my development window, the VuforiaARController code is displayed in red, I think it's because I use the wrong version of Vuforia...

    002.jpg

    Yet I use the version 6.2 but the VuforiaARController.cs script isn't in the package, instead I still have the old script VuforiaBehaviour.cs. as you explain on the page : How To Migrate a Unity Project

    003.jpg

    There is a way to solve the problem with the "old version" ? o_O

    Thanks a lot !
     
  4. Vuforia-Strasza

    Vuforia-Strasza

    Official Vuforia Employee Vuforia

    Joined:
    Jun 13, 2017
    Posts:
    548
    Are you sure you are using Vuforia 6.2.10?

    Either way, it looks like your UpdateState call is in the wrong place. For Cardboard/GVR 0.6.0+ you want to put it before the OnHeadUpdated != null check. The full function that works for me is posted below (using Vuforia 6.2.10, Unity 5.4.1f1, and GVR 1.0.1):

    Code (CSharp):
    1. private void UpdateHead() {
    2.     if (updated) {  // Only one update per frame, please.
    3.       return;
    4.     }
    5.     updated = true;
    6.     GvrViewer.Instance.UpdateState();
    7.  
    8.     if (trackRotation) {
    9.       var rot = GvrViewer.Instance.HeadPose.Orientation;
    10.       if (target == null) {
    11.         transform.localRotation = rot;
    12.       } else {
    13.         transform.rotation = target.rotation * rot;
    14.       }
    15.     }
    16.  
    17.     if (trackPosition) {
    18.       Vector3 pos = GvrViewer.Instance.HeadPose.Position;
    19.       if (target == null) {
    20.         transform.localPosition = pos;
    21.       } else {
    22.         transform.position = target.position + target.rotation * pos;
    23.       }
    24.     }
    25.    
    26.     Vuforia.VuforiaARController.Instance.UpdateState(false, true); // Vuforia 6.2
    27.  
    28.     if (OnHeadUpdated != null) {
    29.       OnHeadUpdated(gameObject);
    30.     }
    31.   }
    Does this work for you?
     
  5. sourabh10995

    sourabh10995

    Joined:
    May 6, 2016
    Posts:
    4
  6. Jjoebstl

    Jjoebstl

    Joined:
    Jan 10, 2018
    Posts:
    9
    Same issue here... Vuforia is great at what it does but it's a bitch to get it to do what you want.