- Home /
Color set to black even though it's set by the editor
You can view the script if you want, but it doesn't help. If I set the color in the script instead of the editor it works fine.
Basically the game object holds an array of this class (in case I make an object with multiple materials later)
public class MaterialColorFadeHandler
{
public var delay : double = 1.0;
public var startColor : Color = new Color();
public var endColor : Color = new Color();
private var myColor : Color = new Color();
private var runTime : double = 0.0;
private var onStart : boolean = true;
private var difference : Color = new Color();
function MaterialColorFadeHandler()
{
if(delay == 0.0)
delay = 1.0; //Don't let delay be 0
myColor.r = startColor.r;
myColor.g = startColor.g;
myColor.b = startColor.b;
myColor.a = startColor.a;
difference.r = endColor.r - startColor.r;
difference.g = endColor.g - startColor.g;
difference.b = endColor.b - startColor.b;
difference.a = endColor.a - startColor.a;
Debug.Log("Start color: " startColor.ToString());
Debug.Log("End color: " endColor.ToString());
}
function runFade(t : double)
{
if(onStart) //Go forwards
{
runTime += t;
if(runTime > delay)
onStart = false;
}
else //Go backwards
{
runTime -= t;
if(runTime < 0.0)
onStart = true;
}
//Alright, now we calculate the color.
var perc : double = runTime / delay;
if(difference.r != 0.0)
myColor.r = startColor.r + (difference.r * perc);
if(difference.g != 0.0)
myColor.g = startColor.g + (difference.g * perc);
if(difference.b != 0.0)
myColor.b = startColor.b + (difference.b * perc);
if(difference.a != 0.0)
myColor.a = startColor.a + (difference.a * perc);
}
function getColor() : Color
{
return myColor;
}
}
Anyway, the color fade (as I mentioned earlier) works when I set the color in the constructor, but not when I set it in the editor (it goes to black, default)
Does anyone know why?
Can you post the code that actually has the array of this class? $$anonymous$$y instinct is that you're initializing this class somewhere, which sets the color to black, ins$$anonymous$$d of just taking in the color from the editor.
Certainly. The code that calls the class is here: #pragma strict
var list : $$anonymous$$aterialColorFadeHandler[];
function Start () {
}
function Update () {
var i : int;
for(i = 0; i < list.Length; i++)
{
list[i].runFade(Time.deltaTime);
renderer.materials[i].SetColor("_Color", list[i].getColor());
}
}
Your answer
Follow this Question
Related Questions
Better way to call function from another script from editor script? 1 Answer
Can I write a script to rename objects as I create them? 1 Answer
Non-MonoBehaviour class exists in scene, constantly calling itself 2 Answers
Fastest way to instantiate and move multiple game objects in the editor 3 Answers
If there is only 1 instance of a class, should it be Static? - C# 2 Answers