2
\$\begingroup\$

I am proceedurally generating a generic game object and trying to make it transparent using the default diffuse transparency shader.

meshRenderer.materials[0]=new Material(Shader.Find("Transparent/Diffuse"));
meshRenderer.materials[0].color = new Color(0f,1f,0f,0.05f);

Just seems to make it Default-Diffuse, but if I do this

meshRenderer.materials[0]=new Material(Shader.Find("Transparent/Diffuse"));
meshRenderer.materials[0].shader = Shader.Find("Transparent/Diffuse");
meshRenderer.materials[0].color = new Color(0f,1f,0f,0.05f);

It works just fine and I get transparency. The Material constructor looks like it takes the exact same shader, but it doesnt seem to be applying. Why? I am already explicitly inluding the shader in the project settings.

\$\endgroup\$
2
\$\begingroup\$

You can change material of your gameObject using:

renderer.material = new Material(Shader.Find("Transparent/Diffuse"));
renderer.material.color = new Color(0f,1f,0f,0.05f);

which seems to work fine. I was searching unity3d forums and found this which also seems to work fine. Here's the code for the same:

var meshRenderer = GetComponent<MeshRenderer>();
var mat = new Material(Shader.Find("Transparent/Diffuse"));
mat.color = new Color(0f,1f,0f,0.05f);
var matArray = new Material[]{mat};
meshRenderer.materials = matArray; 
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.