- Home /
Assigning a mesh through script
Hey all i have a problem with my assigning mesh script, the idea is that on awake it will change the mesh to the appropriate 'weapon' mesh, however there is something wrong.
This script is run on awake
switch(pickupName)
{
case "XM8":
var mesh : Mesh = Resources.Load("Plane002",Mesh);
GetComponent(MeshFilter).mesh = mesh;
break;
}
However it will only make the mesh invisible and when the gameobject is viewed in the inspector the MeshFilter is set to "None".
I am certain that "Plane002" is a mesh, so what is going wrong?
Any help is greatly appreciated ~Myhijim
Did you make sure that the mesh Plane002 is inside a resource folder (i.e. a folder named "Resources")?
I have it within a folder named "Weapons" in the project panel... Am I just being thick?
Ah indeed I am bein thick, it needs to be within a folder in the assets called "Resources" thank you for pointing that out..... Is there any way to load from a different fder?
It is possible but loading from Resources folder is easiest and the recommended way to do so.
Yes you can ;) The path will then be "FolderName/AssetName".
For example, if your hierarchy is like that :
- Resources
|- Props
|- Weapons
|- Plane002.asset
Then the path is "Props/Weapons/Plane002".
Answer by Kryptos · Jul 26, 2012 at 12:36 PM
You have to make sure that mesh Plane002 is inside a resource folder (i.e. a folder named "Resources")?
Can I load from folders within the resource folders?
Yes you can ;-) The path will then be "FolderName/AssetName".
For example, if your hierarchy is like that :
- Resources
|- Props
|- Weapons
|- Plane002.asset
Then the path is "Props/Weapons/Plane002". Thus the correct code would be:
var mesh : Mesh = Resources.Load("Props/Weapons/Plane002", Mesh);
And what if within the model file (modo's *.lxo) I have several meshes and I want to fetch particular one? Triggering Resources.Load("XXX", $$anonymous$$esh)
returns the first submesh of XXX model. Adding "XXX/submesh"
returns no mesh at all
ANSWER: Those sub-meshes that I've created in $$anonymous$$odo Luxology ('n' keyboard shortcut) can be accessed in the following way:
Transform model = Resources.Load<GameObject>("$$anonymous$$odel").transform;
Transform sub$$anonymous$$esh = rocketRes.FindChild("Sub$$anonymous$$esh01"); // etc...