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 Raigex · Dec 24, 2012 at 08:17 PM · androidassetbundle

Why would AssetBundle.LoadAsync give NullReference only sometimes?

I have an asset bundle that I have loaded for my application. Then only sometimes (and I cannot pin down the reason its very random) I get a null reference exception like so:

 12-24 14:44:28.859: I/Unity(2085): NullReferenceException: Object reference not set to an instance of an object
 12-24 14:44:28.859: I/Unity(2085):   at TagTrackableEventHandler+<loadModelAsync>c__Iterator0.MoveNext () [0x00000] in <filename unknown>:0 

[/code]

The only place where I would be moving with iterators (i think) is when getting something from an asset bundle.

Here is the rest of the function of that script private IEnumerator loadModelAsync() {

     AssetBundle bundle = universalHolder.GetComponent<LoadAssetBundle>().returnAssetBundle();
     if(bundle != null && bundle.Contains(this.mTrackableBehaviour.TrackableName) && m3dModel == null && !MemoryCleaner.unloading)
     {
         startAnimation();
         model = bundle.LoadAsync(this.mTrackableBehaviour.TrackableName, typeof(GameObject));
             
         isModelLoading = true;
         yield return model;
         if(model.asset != null)
         {
             if(currentStatus == TrackableBehaviour.Status.TRACKED || 
                     currentStatus == TrackableBehaviour.Status.DETECTED)
             {
     
                 m3dModel = Instantiate(model.asset) as GameObject;
                 yield return null;
                     
                 //Adds model to memory cleaner buffer
                     MemoryCleaner.addModelRender(mTrackableBehaviour.TrackableName);
                     
                 //Makes sure model always follows Image Target
                     m3dModel.transform.parent = this.transform;
                     
                     //Sets ratio based on largest dimension
                 ratio = desiredSize/(Mathf.Max(m3dModel.renderer.bounds.size.x,m3dModel.renderer.bounds.size.y, m3dModel.renderer.bounds.size.z));
                     
                 baseScale.x = ratio;
                 baseScale.y = ratio;
                 baseScale.z = ratio;
                     m3dModel.transform.localScale = baseScale;
     
                     //Calculates y position to simulate standing on image target.
                 float yPosition = ((m3dModel.renderer.bounds.size.y /(m3dModel.transform.lossyScale.y / m3dModel.transform.localScale.y ))/2);
     
                 yPosVector.y = yPosition;
                     m3dModel.transform.localPosition = yPosVector;
                     
                     //Sets base rotation (180 degrees around y axis)
                     m3dModel.transform.localRotation = Quaternion.AngleAxis(baseRotation, Vector3.up);
     
                     m3dModel.renderer.enabled = true;
     
                 m3dModel.layer = LayerMask.NameToLayer("ARLayer");
                     
                     //Add a collider for touch actions
                     m3dModel.AddComponent<BoxCollider>();
                 m3dModel.name = mTrackableBehaviour.TrackableName;
                     
                     //Adds the ModelBehaviour Component to react to touches.
                     m3dModel.AddComponent<ModelBehavior>();
     
                     m3dModel.GetComponent<ModelBehavior>().setModelRatio(ratio);
                     
                 isModelLoading = false;
                     
                     //Clears releases memory of model loaded from asset bundle;
                 model = null;
                     
             }
         }
     }
         
     //Stops targeting Animation
     targetingAnimation.stop();
     //Hides Animaiton
     scanTargeting.hidden = true;
 
     //Stops position update of animation.
     isAnimPlaying = false;
         
 }

Also the weird thing is it doesn't always happen. Sometimes the same model (a bear in this case) will load up 10 times in a row without any problems then i will get the above error and I will not be able to instantiate it again.

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
Best Answer

Answer by Raigex · Dec 28, 2012 at 08:11 PM

I think I have found the answer (accidentally). The null reference exception is because of my

 return yield null;

And the reason why it sometimes didn't want to load up is because when the model is loading and it it gets stopped prematurely the isModelLoading will not get set to false (as it is finished loading) which stops this whole loadModelAsync() from being called again ( I have another function that calls loadModelAsync if the model is not loading). I have fixed these issues and it no longer gives me the exception or stops me from loading.

As for why i get that null reference exception is because set

 _m3dModel = null

in a function that gets called when I loose the tracking (when i find the tracking the loadAsync function gets called). Since I start instantiating a model then i set it to null all codes after my return yield call that use _m3dModel no longer work.

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

8 People are following this question.

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

Related Questions

changing image format when building scene assetbundle 0 Answers

Images Not Displaying Correctly On Android 0 Answers

Crash on iOS after upgrading to Unity 4 1 Answer

Could not get main asset from WWW object 0 Answers

Assetbundle loaded asset cannot be cleaned in Android 1 Answer


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