• 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 Zach_Stoltz_PSU · Sep 04, 2012 at 03:56 PM · c#errornullreferenceexceptionaugmented reality

NullReferenceException

This may be a simple fix due to the error being thrown is a NullReferenceException. I am unable though to find where the actual error is in the code where the compiler thinks the error occurs. The two Scripts that are being used are from STRING Augmented Reality so the C# scripts are a little new to me.

I have marked the error with Asterisks to show the line of code where the error is.

Hopefully someone here can help! Thanks

 public class DrawingUI : MonoBehaviour {
 public Camera mainCamera;
     public Camera stringCamera;
     private bool isMainCameraEnabled = false;
     public bool isStringCameraEnabled = false;
 
 // Use this for initialization
     void Start () {
         mainCamera.enabled = true;
         isMainCameraEnabled = true;
         
         isStringCameraEnabled = false;
         stringCamera.enabled = false;
     
     }
     
     // Update is called once per frame
     void Update () {
         if(isMainCameraEnabled == true)
         {
         mainCamera.enabled = true;
         stringCamera.enabled = false;
         }
         
         if(isStringCameraEnabled == true)
         {
         stringCamera.enabled = true;
         mainCamera.enabled = false;
         }
     
     }
     
     void OnGUI(){
     infoWindow = GUI.Window(1,materialsWindow, materialsWindowFunc, "Material Window");
 
 
     
     GUILayout.BeginArea(new Rect (Screen.width / 2 - backButtonWidth /2, Screen.height / 2 - backButtonHeight/2 + 900 - backButtonSpacing,
                              backButtonWidth, backButtonHeight));
         
         if(GUILayout.Button("View Model", GUILayout.Height(backButtonSpacing)))
         {
             isStringCameraEnabled = true;
             isMainCameraEnabled = false;
             cameraCentricARManager.StartString();
             
             
             
             //Application.LoadLevel("StringUI");    
         }
         
         if(GUILayout.Button("Back to Main Menu", GUILayout.Height(backButtonSpacing)))
         {
             Application.LoadLevel("MainMenuUI");
         }
     
     GUILayout.EndArea();
 }


 public  class CameraCentricARManager : MonoBehaviour {
     public GameObject[] rootObjects;
     public bool fullscreen = false;
     public float alignment = 0.5f;
     public bool reorientBranding = true;
     float[] lastSpottedTimes;
     
     public StringWrapper stringWrapper;
     
      public void StartString() 
     {
         // Initialize String
         stringWrapper = new StringWrapper(null, camera, reorientBranding, fullscreen, alignment);
 
         // Load some image targets
         for (uint i = 0; i < rootObjects.Length; i++)
         {
             stringWrapper.LoadImageMarker("Marker " + (i + 1), "png");
         }
 
         // Hide all rootObjects
         // Also, set them as children of the camera;
         // This is to more easily position them relative to the camera later
         for (uint i = 0; i < rootObjects.Length; i++)
         {
             rootObjects[i].SetActiveRecursively(false);
             rootObjects[i].transform.parent = transform;
         }
         
         // Allocate array to track the last time each marker was spotted
         lastSpottedTimes = new float[rootObjects.Length];
         
         // Prevent the iOS keyboard from introducing an unwanted
         // black frame when rotating
         iPhoneKeyboard.autorotateToLandscapeLeft = false;
         iPhoneKeyboard.autorotateToLandscapeRight = false;
         iPhoneKeyboard.autorotateToPortrait = false;
         iPhoneKeyboard.autorotateToPortraitUpsideDown = false;
     }
     
     void CreateWrapper() 
     {
     ********    //Error occurs here, Located in StringWrapper Update Method ***********
         // Perform marker image tracking
         uint markerCount = stringWrapper.Update();
         
         // Handle detected markers
         for (uint i = 0; i < markerCount; i++)
         {
             // Fetch tracker data for this marker
             StringWrapper.MarkerInfo markerInfo = stringWrapper.GetDetectedMarkerInfo(i);
             
             if (markerInfo.imageID < rootObjects.Length && rootObjects[markerInfo.imageID] != null)
             {
                 rootObjects[markerInfo.imageID].transform.localPosition = markerInfo.position;
                 rootObjects[markerInfo.imageID].transform.localRotation = markerInfo.rotation;
                 lastSpottedTimes[markerInfo.imageID] = Time.time;
                 
                 if (!rootObjects[markerInfo.imageID].active)
                 {
                     rootObjects[markerInfo.imageID].SetActiveRecursively(true);
                 }
             }
         }
         
         // Deactivate rootObjects for lost markers
         for (uint i = 0; i < rootObjects.Length; i++)
         {
             if (rootObjects[i].active)
             {
                 if (Time.time - lastSpottedTimes[i] > 2)
                 {
                     // Marker has been out of view for a while; Deactivate it
                     rootObjects[i].SetActiveRecursively(false);
                 }
                 else if (Time.time != lastSpottedTimes[i])
                 {
                     // Marker wasn't spotted this frame; Hide it
                     rootObjects[i].transform.localPosition = new Vector3(1000000, 0, 0);
                 }
             }
         }
     }
 }

This is the method from StringWrapper where the error happens

 public uint Update()
     {
         if (wasInitialized)
         {
             if (isMobile)
             {
                 if (Screen.orientation != currentOrientation && Time.frameCount > 1)
                 {
                     ApplyViewSettings();
                     
                     MobileWrapper.String_SetProjectionAndViewport(_camera.projectionMatrix, _camera.rect, (int)(currentOrientation = Screen.orientation), _reorientBranding);
                 }
         
                 markerCount = MobileWrapper.String_GetData(markerInfo, maxMarkerCount);
             }
             else
             {
                 if (DesktopWrapper.IsNewFrameReady())
                 {
                     DesktopWrapper.ProcessFrame((uint)videoTexture.GetNativeTextureID(), 0);
                     markerCount = DesktopWrapper.GetDataQuaternionBased(markerInfo, maxMarkerCount);
                     videoPlaneObject.active = true;
                 }
             }
         }
         else
         {
             // Write dummy output data
             markerCount = 1;
             markerInfo[0].DummyData();
         }
         
         return markerCount;
     }
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
0

Answer by DaveA · Sep 04, 2012 at 04:33 PM

When is StartString called? My guess is either never, or after this Update function is called. Make sure it is called before that Update. Either call it Start or call it FROM Start, or edit the script execution order to put that class first.

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 Zach_Stoltz_PSU · Sep 06, 2012 at 04:37 PM 0
Share

Thanks for the response Dave, I realized what I was doing wrong. I did not put my third script in thinking it was not a factor but it seems it is. I am going to update my code above with the 3rd script! Thanks

avatar image Zach_Stoltz_PSU · Sep 06, 2012 at 04:43 PM 0
Share

An Update as well, the problem is no longer with Null Exception, that was fixed by renaming the Update method CreateWrapper

The problem now is that the actual String Camera will not function.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

C# Error help ( error CS0023 The `!’ operator cannot be applied to operand of type `string’ ) ??? 1 Answer

NullReferenceException problem 2 Answers

NullReferenceException: Object reference not set to an instance of an object player.Update () (at Assets/player.cs:10) 1 Answer

The name 'TrackManager' does not exist in the current context [Unity Vuforia Ground Plane Project] 0 Answers

Unexplainable NullReferenceException 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges