- Home /
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613085906im_/https://answers.unity.com/themes/thub/images/avi.jpg)