- Home /
Changing the alpha of Mesh Renderer
Hi, I am trying to make a certain item blink when time is almost expired. I want to change the alpha to 0 but the opacity does not change at all.
I did it this way:
itemWeapon.GetComponentInChildren<MeshRenderer>().material.color = new Color(1f,1f,1f,0f);
I tried to print the color out and it says RGBA(1.000, 1.000, 1.000, 0.000) but the object is still very visible.
Answer by Eric5h5 · Apr 01, 2014 at 02:34 PM
You need to use a shader that supports transparency, but it would be better to simply disable the renderer instead.
how do I know if my shader supports transparency. I just used the default mesh renderer after I imported it from blender.
I tried disabling the renderer but only one child of my object is disabled. How do I disable the rest of the children in my mesh renderer?
I used
itemWeapon.GetComponentInChildren().renderer.enabled = false;
do I need to loop it to get all the children?
fixed it by doing this
for(int i=0; i< itemWeapon.GetComponentsInChildren<$$anonymous$$eshRenderer>().Length; i++){
itemWeapon.GetComponentsInChildren<$$anonymous$$eshRenderer>()[i].renderer.enabled = false;
}
is there a more optimized solution to this?
A more optimized version would be:
List meshRenderers = itemWeapon.GetComponentsInChildren<$$anonymous$$eshRenderer>();
for(int i=0; i< meshRenderers .Length; i++){
meshRenderers[i].renderer.enabled = false;
}
Answer by smashingy0u · Apr 01, 2014 at 02:49 PM
Your code is perfectly fine. The shader on your object doesn't support alpha. i tried transparant/ Bumped diffuse and that one seemed to work. you can try other shaders for yourself ;)
edit: to add to my comment i'd assume that you already have a material added to your gameobject?
I do have a material added. In fact I have 6 materials on my gameobject. How can I use other shaders? Do you mean add a shader component to my gameobject?
No, the shader is an attribute in the material tab in the inspector. its a drop down box
Answer by Shadoninja · Jul 07, 2018 at 01:33 AM
To make @smashingy0u's answer a little clearer, as long as there is a material on your Mesh Renderer, you can just do something like this to make a text mesh invisible:
TextMeshGamObject.GetComponent<MeshRenderer>().material.color = new Color(1f, 1f, 1f, 0f);
Your answer
![](https://koobas.hobune.stream/wayback/20220613141702im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make a "Click to Continue" text message? 1 Answer
Strange artifacts on Vertex Color Shader 0 Answers
Change Alpha of Text via Code? 1 Answer
Can't animate Material.Color properties 3 Answers