material color alpha doesn't seem to work.

I'm doing a simple box-add, and the alpha in the color doesn't seem to work. Code:

  1. var fieldBox : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
  2. fieldBox.transform.parent = this.transform;
  3. fieldBox.transform.renderer.material.color = Color.blue;
  4. fieldBox.transform.renderer.material.color.a = 0.05;

It just comes out solid blue. I'd expect something that was 95% transparent. What simple n00b mistake am I making?

Thanks!




Answer by Olie 

The immediate answer to the question that I asked is: the default shader does not do transparency. +1 to Eric5h5 for that.

However, the useful answer that I was looking for (and eventually found, but after a long-ish search!) was that I want to include this line:

  1. fieldBox.renderer.material.shader = Shader.Find( "Transparent/Diffuse" );

In the code above. So the whole thing looks like this:

  1. var fieldBox : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
  2. fieldBox.transform.parent = this.transform;
  3. fieldBox.transform.renderer.material.color = Color.blue;
  4. fieldBox.transform.renderer.material.color.a = 0.05;
  5. fieldBox.renderer.material.shader = Shader.Find( "Transparent/Diffuse" );

There are, of course, a variety of shaders and more efficient ways to get at them than Find(), but this is the answer for someone just looking to put a transparent cube on their early-on n00b project -- "how hard can it be?!"

I'm sorry you didn't find my answer sufficient, but I did say "You need to use a shader that has transparency." The second part ("The default diffuse shader doesn't") was merely a qualifier to the more important first part. I figured it would have been fairly obvious that you'd take another look at the shader drop-down list, see the "Transparent" section there, and say "Oh, I get it." Because that is exactly the reaction of several people in the past who've asked this question and gotten the same answer from me. I try to provide only the relevant info rather than wasting your time with fluff.

Also, if you find an answer to be unclear, just post a comment under it saying so, and I'll expand it.

Eric: yeah, good points all. Perhaps I wasn't clear that I was trying to do all of this in script, at which point "use a different shader" is, as I said, technically correct but not helpful. I'm not trying to "start up" with you -- I just noticed that a very many of the answers on this site are like that. I come from a strong StackOverflow background, where the answers are typically more overt, it all. No worries, I'll get the hang of things around here.




















ref : https://answers.unity.com/questions/33556/material-color-alpha-doesnt-seem-to-work.html

반응형

+ Recent posts