Loading resources and potential bug

Okay, bear with me here, I’m new to Unity (although not to programming) so I have questions here and there.

The gist of what I’m trying to do is a pretty basic script to load some resources to be used sent to a mesh filter. The effect I’m looking for is basically loading different equipment depending on what the player has selected in a previous screen.

The reason I want to load these things from the resources folder and not just hardcode them into some array is because I assume everything I put into the scene hierarchy will be loaded into memory upon startup? It’s not a problem now but it would be with lots of different weapons and helmets so I want it done right from the start. So: Question #1: Does adding meshes to an array in the hierarchy mean the meshes are loaded into memory at scene start?

So what I’ve done is I got some models in the Resources-folder from which I want to fetch the meshes. If I’m doing something retarded here please point it out! For the headgear, I have a script called MadHatter which simply fetches a string from another game object, and based on that string loads the appropriate resource. The resource in this case is an .obj model btw. The mesh resides in a child. Here comes the strange and buggy part I just can’t wrap my head around!

First I wrote (C#):

GameObject resource= (GameObject)Resources.Load("elfinaxe");
MeshFilter lolMesh = (MeshFilter)resource.transform.Find("BRONZE").GetComponent("MeshFilter");

MeshFilter filter = (MeshFilter)gameObject.GetComponent("MeshFilter");
filter.mesh = lolMesh.mesh;

This worked, since I could see the name of the child containing my mesh, but it felt stupid to have to hard code the name even though there was only one child of interest! So I found the method called “GetComponentInChildren” and thought I had found a solution.

I replaced the resource.transform.Find etc. with

MeshFilter lolMesh = resource.GetComponentInChildren<MeshFilter>();

Unfortunately, this function couldn’t find any MeshFilters? How does it work if it can’t find a filter I can fetch manually in a child?

Oh well, I thought, and pressed CTRL+Z a few times until everything was back to the way it was when it worked.

And it no longer worked! I did the same thing to my weapon-swapping script, and it happened again. I can see that the mesh is in fact fetched and put into the mesh filter of the MadHatter, but it does no longer render! Also note I can click the circle thingy next to the Mesh and select the same mesh manually, and it will work.

Halp plz! =(

You help me if you know what the hell is going on, OR if you know a better way to achieve dynamic equipment swapping!

Okay, after a good night’s sleep and some further tinkering I figured out why it behaved this way…

When I load the asset, and assign the mesh to my array, for some reason it’s a reference TO THE PREFAB that gets assigned (even though the documentation suggest it would be a copy). When I stop the game, the mesh in my array gets freed and so does the mesh in my prefab. So it only works the first time because between the first and second run Unity utterly ruins my prefab in the Project-window. ಠ_ಠ

The “solution” to this was to use “sharedMesh” instead, despite the documentation stating it would behave like “mesh” infact does.

Oh, well. That still doesn’t solve the fact that “GetComponentInChildren” isn’t even remotely working. I think I’ll have to figure out a different approach.

EDIT: Okay, I think I get why GetComponentInChildren isn’t working. Apparently it can only find what it calls “active components”, and I’m guessing my recently loaded object isn’t active at all.