This is what I’ve been trying to do, and here’s what I’ve seen so far:
gameObject.GetComponent<Renderer>( ).material.SetColor( "Main Color", new Color( 42.0f / 255.0f, 40.0f / 255.0f, 120.0f / 255.0f ) );
Debug.Log( "material " + gameObject.GetComponent<Renderer>( ).material.name + " has " + ( gameObject.GetComponent<Renderer>( ).material.HasProperty( "Main Color" ) ? "" : "NOT " ) + "property Main Color" );
As you can see, here I simply want to assign to the Color property called “Main Color” the colour (42,40,120).
I never got this to work so I tried to check if the property actually exists with this name, hence my debug output which gives this:
material blue (Instance) has property Main Color
So the property DOES exist with this name, (and I assume) sees its value set to (42,40,120), and yet, nothing happens at all, the colour stays like it is in the prefab.
However, as the game is running, would I try to change the “Main Color” property on this particular instance via the Inspector, it WILL change.
Also, if I do this:
gameObject.GetComponent<Renderer>( ).material.color = new Color( 42.0f / 255.0f, 40.0f / 255.0f, 120.0f / 255.0f );
Then it works and the colour does change. But it seems that changing a shader/material property via the code isn’t quite working, unless I missed something.
And I will very soon need to be able to change some material/shader properties via the code, ones that aren’t directly member of the Material structure, like Material.color or Material.mainTexture are.
Help!
Cheers.