- Home /
c#: Best practice to toggle visiblity of meshes within a gameObject?
I'm brand new to Unity and have read through tutorials but have not 'clicked' yet with best practices for manipulating the components of a gameObject.
I have exported three meshes from a max file. Mesh01, Mesh02, Mesh03
I wrote a simple c# script that currently contains information with regards to the individual meshes (Mesh class), but labelled them: Healthy, Damaged, Wreck and then plug in the meshes into the vars. I then have a health var and, for the purposes of testing will slowly reduce till it reaches zero.
My problem is that I am unsure how to control the "Mesh Renderer" for each mesh.
Healthy.enabled and Healthy.renderer do not appear to exist at the mesh level. Any pointers would be helpful :).
Thanks,
J.
Answer by CodeArtist_MX · May 15, 2013 at 10:09 PM
Ok, so i think i get what your problem is... First of all if you only want a GameObject which contains your model so you can add scripts to it i think you should start by using .fbx files instead of max, since they are more simple to manipulate in unity (unity will create a prefab of that model with all you need and you only have to drag and drop it to the scene, it will render automatically). I Hope this link is helpful:
However according to what i understand you created a script that holds a public "Mesh" type variable and you are trying your .max model into it. There are 2 important things about this:
Any script that you are planning to use in the scene must be attatched to a GameObject , you can create a empty one and attatch the script you created into it. (Your script should extend from Monobehavior if you want to use the GameObject properties like ".active", that disables the gameObject completely from the scene)
The gameobject that holds the script must have attatched a "Mesh renderer" component in order to render a mesh, you can attatch it from Component->Mesh->MeshRender , also as "whydoidoit" said in another answer you will need a MeshFilter which you can add to your gameObject in Component->Mesh->MeshFilter
You can Enable/Disable the meshfilter like:
gameObject.GetComponent<MeshRenderer>().enabled=false;
After that im a little bit lost to be honest, since i used this approch to dinamycally make meshes, however if you case you have to load your model by hand using Resources.Load (and according to what i think it should be automatically displayed after that).
This second approach will complicate your workflow a lot and also will make you unable to visualize your model while rendering the scene, since you have to Run your script to Load it.
I would stick to using the .fbx files and drag and dropping the prefab :)
I hope i answered you question and that it helps you in your project!
GameObject doesn't have an enabled property - it has activeInHierarchy and activeSelf. Only things derived from Behaviour (and a few other components) have an enabled property.
Thanks! sorry for the mistake, you are completely right, i will fix my answer.
First off, thanks for the replies. However, I'm still unsure about implementation.
To clarify:
I export the max file as FBX -- and the result in unity is a prefab that contains three meshes with a simple shader/material.
In Unity, a script allows the user to specify the meshes as being a particular type at the top of the prefab, such as Healthy or Damaged or Wrecked.
There is a variable -- lets call it health, that counts down slowly so that on update I can trigger changes based on its value.
The user, for the moment, goes in and sets the health they want to give to the prefab, then they slot in the meshes they want to use for Healthy, Damaged, and Wrecked. When play is pressed the update function checks the health and depending on its value will show the appropriate mesh.
Currently the following works to update the mesh, but I'm concerned about iterating through all the mesh children. This seems inefficient:
void update$$anonymous$$eshState(string meshname)
{
foreach(Transform temp in transform)
{
if(temp.name == meshname)
temp.renderer.enabled = true;
else
temp.renderer.enabled = false;
}
}
I've attempted to use the $$anonymous$$eshRenderer and the $$anonymous$$eshFilter -- but with no luck so far. This function I've provided seems costly since I'm having to iterate through all meshes -- I'd rather just be specific and use that $$anonymous$$eshFilter idea.
Answer by whydoidoit · May 15, 2013 at 10:46 PM
A MeshRenderer is attached to a GameObject and the mesh it renders is controlled by a MeshFilter that you add to the same GameObject. You can set the mesh of the MeshFilter to cause the MeshRenderer to show a different mesh.
Your answer
Follow this Question
Related Questions
Turning off renderer in other game object (C#) 2 Answers
Can't remove component. MeshRenderer depends on it 1 Answer
appear/disappear gameobject 3 Answers
Game object with animations 0 Answers
Can't render GameObject 3 Answers