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!