I have a code that loads a resource at the runtime using Resources.Load, and that resource is a prefab (in the Resources folder).
When I call this code at the start of my level, the resource is loaded and everything works fine.
When my level is complete I Destroy the object (that is loaded at the start) and call UnloadUnusedAssets.
But when I start the level again, and call the same code (from the beginning), Resources.Load returns null (even though I am loading the same resource that worked fine the first time).
Also this does not happen in the editor, just on the iPad.
In this case, you set this new object as the child of an object in your Scenegraph/Hirachy before you do the instanciation. Here the complete object is then discarded, when you unload your scene. I think some of the resource is also discarded in this case. As Unity does not recreate the resource after it has been loaded once, the resource-object gets a bit messed up. When you try to load it again, it does not recognize it anymore as a GameObject, thus it is not loaded and the result is null.
Instead of the code above, you should instanciate your loaded GameObject before you set the parent property on the cloned Object:
GameObject chair = (GameObject)Resources.Load("chair", typeof(GameObject));
GameObject c = (GameObject)Instantiate(chair);
c.transform.parent = gameObject.transform;
c.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
I also have this behavior. I’ve a GameObject in the “Resources” folder which I can load one time:
car = (GameObject)Resources.Load("chair");
Then the level quits and I restart the scene. This line is executed again, but this time the Resources.Load(“chair”) function does not return a GameObject, it returns a Mesh-Object.
I found that if you Destroy the asset, Resources.Load will return null for that asset from then on. Instead you need to call Resources.UnloadAsset.
Instead of:
TextAsset bindata = Resources.Load("SomeAsset") as TextAsset;
DoSomethingWith(bindata);
Destroy(bindata);
// Resources.Load("SomeAsset") will return null from now on
Do this:
TextAsset bindata = Resources.Load("SomeAsset") as TextAsset;
DoSomethingWith(bindata);
Resources.UnloadAsset(bindata);
// Resources.Load("SomeAsset") will succeed again next time