- Home /
Color index operator not working properly
Unanswered as of Feb 20, 2014 11:50am PST.
I am trying to use the Index operator on the material color for a basic object using the default material. Here is the code I am trying to make work but for some reason isn't:
var i : int;
for (i=0; i<4; i++) {
if (this.rgba[i] > 1) {
this.rgba[i] = 1;
}
else if (this.rgba[i] < 0) {
this.rgba[i] = 0;
}
this.renderer.material.color[i] = this.rgba[i];
}
Based on This Link it should work the same as the follow (Which does work):
var i : int;
for (i=0; i<4; i++) {
if (this.rgba[i] > 1) {
this.rgba[i] = 1;
}
else if (this.rgba[i] < 0) {
this.rgba[i] = 0;
}
}
this.renderer.material.color.r = this.rgba[0];
this.renderer.material.color.g = this.rgba[1];
this.renderer.material.color.b = this.rgba[2];
this.renderer.material.color.a = this.rgba[3];
Could someone explain why this isn't working for me.
I am calling this in my update function.
rgba is a instance variable float[4];
Using JavaScript.
Unity version 4.3.3f1 on Windows 7
Sources that my logic is based on
http://docs.unity3d.com/Documentation/ScriptReference/Color.Index_operator.html http://docs.unity3d.com/Documentation/ScriptReference/Material.html http://docs.unity3d.com/Documentation/ScriptReference/Color.html
Update
I have tried another method of achieving this (that works) but I still don't know why the first method in the post isn't working (In JAVASCRIPT).
var i : int;
var c : Color = this.renderer.material.color;
for (i=0; i<4; i++) {
if (this.rgba[i] > 1) {
this.rgba[i] = 1;
}
else if (this.rgba[i] < 0) {
this.rgba[i] = 0;
}
c[i] = this.rgba[i];
}
this.renderer.material.color = c;
Answer by rutter · Feb 19, 2014 at 10:18 PM
Same problem as on this Stack Overflow thread. I'll post a similar answer here.
In C#, structs are passed by value.
When you get material.color
, you're getting a copy of the material's color; changes made to the copy do not alter the original.
The following doesn't work, because you're modifying and discarding a copy:
renderer.material.color.a = 0;
The following does work, because you're getting a copy, modifying it, and passing it back:
Color tempColor = renderer.material.color;
tempColor.a = .25f;
renderer.material.color = tempColor;
That is a different problem. $$anonymous$$y problem is using using the index operation to change the color vs changing the individual r g b or a as described in this page http://docs.unity3d.com/Documentation/ScriptReference/Color.Index_operator.html
I A$$anonymous$$ ABLE to change the using this.renderer.material.color.a = some number. Also I am using Javascript.
Also Color is a Class not a Struct because it has Static Variables, Constructor, Operator overrides, and function that are used on the Class or variable with a dot accessor.
@geozak: no, Color is a struct. Structs can also have a constructors, member functions and most things classes have. Also Unityscript might cheat on you here. They implemented some syntactical sugar for r, g, b, a values.
When you write:
this.renderer.material.color.r = 0.5;
The compiler actually does this behind the scene:
var tmp = this.renderer.material.color;
tmp.r = 0.5;
this.renderer.material.color = tmp;
Those kind of syntactical sugar isn't implemented for all value-types but only for certain, built-in types.