- Home /
How to FadeIn 3dObject using alpha color?
Hello everyone I know this question was asked a multiple times, but non of the solutions from other question gived me a result I want.
Here is the code example that I tried first, it changes the value of RGBA but it's not returning them to material, basically I don't understand how to return "Color" to material.
Color col = GetComponent<MeshRenderer>().material.color;
col.a = 0.4f;
Debug.Log(col.ToString());
And a second example just gives me a material error.
Renderer rend = GetComponent<Renderer>()
rend.material.SetColor("_SpecColor", new Color(1, 1, 1, 0.5f));
Anyone can you please give some advice.
Answer by Harardin · Nov 30, 2016 at 08:41 AM
Sorry for double question, found the way of doing it. in answer of @joe_bar from this question: http://answers.unity3d.com/questions/60453/fading-out-using-rendermaterialcolora-doesnt-work.html
Here is the code excample that worked for me:
Renderer rend = GetComponent<Renderer>();
rend.material.color -= new Color(0, 0, 0, 0.9f);
Sorry for self answering.
Adding one more thing for those who going to use it in "for" loop use it with double.
for (double i = 0; i < 1.0;)
{
i = i - 0.1;
float a = (float)i;
GetComponent<Renderer>().material.color = new Color(1, 1, 1, a);
}
Then it will work properly.
Also to FadeOut use it like this this from my Corrutine: GetComponent().material.color = new Color(1, 1, 1, colConv);