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:

var fieldBox : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
fieldBox.transform.parent = this.transform;
fieldBox.transform.renderer.material.color = Color.blue;
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!

1 Like

2 Answers

2

You need to use a shader that has transparency. The default diffuse shader doesn't.

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:

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

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

var fieldBox : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
fieldBox.transform.parent = this.transform;
fieldBox.transform.renderer.material.color = Color.blue;
fieldBox.transform.renderer.material.color.a = 0.05;
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?!"

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.