Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 2 down vote accepted

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; 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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