Material instance won't change color?
I'm trying to and change the color of a trail renderer. I created this workaround, but I'm running into an odd issue. The material instance's color is changed in the Unity inspector, but it won't change color in the game unless I manually use the color picker and change the color (and sometimes, even that does nothing). What am I doing wrong?
var Test: Material;
var MyColors: Color[];
function Start(){
Test = GetComponent.<TrailRenderer>().material;
Test.SetColor ("_TintColor", MyColors[1]);
}
Answer by LazyElephant · Dec 15, 2015 at 12:37 AM
If you remove the . before the <TrailRenderer>
and reformat the variables and start function to c#, this does run as a c# script. I'm not too familiar with javascript, but after checking the Unity reference page for GetComponent, it appears that the javascript version uses the syntax GetComponent( string name)
instead of GetComponent<component>()
. To get your code working, I changed it to the following
var test: TrailRenderer;
var myColor: Color;
function Start () {
test = GetComponent("TrailRenderer");
test.material.SetColor("_TintColor", myColor);
}
No, not really. You shouldn't use the string version in any language as the return type will be Component in that case. UnityScript will automatically cast it if you pass it to a typed variable, but the string version is much slower than the generic or the type inferred version.
The generic syntax is perfectly fine. It looks like this in UnityScript. The code in question should compile without any errors. However in UnityScript it's usually better to use the type-inferred version. So ins$$anonymous$$d of:
Test = GetComponent.<TrailRenderer>().material;
you can do:
Test = GetComponent(TrailRenderer).material;
In UnityScript passing a "type" as parameter will implicitly pass typeof(type). Furthermore the compiler inferes the return type from the given parameter even though it's not a generic method. It's a feature of the UnityScript compiler. Though both versions should work.
Apart from pure syntax, there could be other problems which usually should result in some kind of runtime error or warning.
The code sets the color only once when the game is started. Any change of the myColors array while the game is running won't affect the trail renderer.
You always set the color to the second color in the array (index 1). So why using an array in the first place? $$anonymous$$aybe it's just an example, but it just looks strange. Also make sure you have at least two colors in the array, otherwise you get an out of bounds error.
You set the shader property called "_TintColor". Only a few shaders use this property, usually the particle shaders. If you use a different shader that doesn't have that property of course your code will have no effect.
I'm going to assume it was my general lack of Unityscript knowledge that caused it, but when I tried using his code, I was unable to get it to work, even with the proper shader attached. However, after reading your comment and trying again, I was able to get his code to work as is. I'm not sure what i did wrong the first time.
Every time you post something, it's always very in depth and I learn something new. $$anonymous$$eep up the good work. You should post this comment as an answer, since it's more accurate than $$anonymous$$e.
Your answer
