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 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 rena$$anonymous$$g 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.

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

NullReferenceException problem 2 Answers

Multiple Cars not working 1 Answer

I cannot understand where the Object reference is not set to an instance of an object? 1 Answer

NullReferenceException yet Debug.Log shows nothing is null? 1 Answer

Editing in Animator removes Player tag 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