Setting Material Color

I am trying to set the color on a material via scripting, but is doesn’t work. This is the line that should do it:

renderer.material.SetColor("_Emission", Color.red);

The shader I am using is particle/vertex lit. Any ideas why this won’t work? It works on normal vertex lit.

Look in the shader and see what the property is called, if “_Emission” isn’t working.

–Eric

This doesn’t exist unless you made it yourself. If you are actually using a builtin shader, please tell us which one it actually is. If you are using your own shader, please post it, and I’ll see if I can figure that out.

If you’re talking about “Particles/VertexLit Blended”, however, then you need this:

renderer.material.SetColor("_EmisColor", Color.red);

I am having the same problem…
pasted in the code from the unity scripting manual…
Tried every option for color name… but it always shows up white…
I debug logged the property, and the values are what I set it to.

I can click on the object while its running and change the material color just fine.

but using this script doesn’t create a material with the defined color… its always white, what is wrong with it please ?

string shaderText =
“Shader "Alpha Blended" {” +
“Properties { _Color ("Main Color", Color) = (160,170,10,80) }” +
“SubShader {” +
" Tags { "Queue" = "Transparent" }" +
" Pass {" +
//" Blend One One ZWrite Off ColorMask RGBA" + //Blend One One
" Blend SrcAlpha OneMinusSrcAlpha ZWrite Off ColorMask RGBA" +
" Material { Diffuse [_Color] Ambient [_Color] }" +
" Lighting On" +
" SetTexture [_Dummy] { combine primary double, primary }" +
" }" +
“}” +
“}”;

myMaterial = new Material(shaderText);

//none of these work, not the color above… its always white ?
//myMaterial .SetColor(“Main Color”, new Color(160, 170, 10, 80)); !!!
//myMaterial .SetColor(“_Color”, new Color(160, 170, 10, 80));
//myMaterial .SetColor(“color”, new Color(160, 170, 10, 80));
//myMaterial .SetColor(“_Emission”, new Color(160, 170, 10, 80));
//myMaterial .color = new Color(160, 170, 10, 80);

I can’t read that. Did you try making an actual shader file, a material to go along with it, and testing that?

no, I coped it from the unity scripting docs.

I don’t want to make a shader file, as I want this to be dynamic material created, without having to drag and drop it onto a object/slot.

if I make a shader… can I still find it and add it to dynamically created primitives at run time ?
That about with different colors ?

thx for reply

Of course you can. Just use the second method instead of the first.
http://unity3d.com/support/documentation/ScriptReference/Material.Material.html

I actually don’t know why you’d create one with text, though I believe I’ve seen Eric do that, so maybe he can enlighten us. I hate looking at that text-in-code garbage, personally.

thanks…
interesting… I got method 2 working… but with the same result… its always white…

It correctly sets the correct shader., the object shows up, the script is running, not errored out.
I just cant figure out how to set the color ?
any ideas ?

GameObject cyl = GameObject.CreatePrimitive(PrimitiveType.Cylinder);

Shader shadr = Shader.Find(“Transparent/Diffuse”);

cyl.renderer.material = new Material(shadr);

cyl.renderer.material.color = new Color(160, 170, 10, 80);
cyl.renderer.material.SetColor(“_Color”, new Color(160, 170, 10, 80));
cyl.renderer.material.SetColor(“MainColor”, new Color(160, 170, 10, 80));

cyl.renderer.material.SetColor(“Main Color”, new Color(160, 170, 10, 80));

maybe I cant set the color on an object right after its been instantiated/created… ? do I have to wait until after its own Awake() or Start() function is called ?
or maybe it needs a texture set ?

Two things.

1.  Use the Code button in your posts.
  1. Read the top of this page. Of course everything is white! The value of each color component is clamped at 1.

hah hah, doh !
thanks

well might I suggest that Unity fixed their color picker to show 0 to 1, instead of 0 to 255 then in next unity version :wink:

thanks muchly