- Home /
transparency and color change in individual GO
I have an empty game object prefab with many children. Each child is a cylinder with bumped diffuse shader. I want to (in C#):
a) change the color of each child to green in one individual empty game object, while there are many clones of this prefab in the scene
b) make all children in one individual empty game object semi-transparent (so I can see them and through them, like a ghost)
I have a reference to the parent empty game object in my script.
Answer by Rafes · Jul 19, 2011 at 11:24 PM
To change just an instance's material, just use renderer.material. Only using shaderMaterial will set everything with that material. Once you have that, then it sounds like you want to do a recursive material set.
Before I post anything further, a warning... be aware that Unity will not batch render objects once you set their material because it is like making a copy of the material, and Unity only batches objects with the same material
I suggest, in awake, you cache all objects which have a renderer (mesh renderer) in a list. then it is much faster to run through the list. You can do this with a recursive function which will add anything found and continue looking until there are no more children. I can't test this code, but it should be something like this:
private List<Renderer> childRenderers;
private void Awake()
{
this.PopulateChildRenderers(this.transform);
)
private void PopulateChildRenderers(Transform xform)
{
Renderer rndr = xform.renderer;
if (rndr) this.childRenders.Add(rndr);
foreach (Transform child in xform)
this.PopulateChildRenderers(xform);
}
Then just go through the list and do whatever you need to, whenever you need to.
Your answer
![](https://koobas.hobune.stream/wayback/20220612151157im_/https://answers.unity.com/themes/thub/images/avi.jpg)