# How can I scale a color component by Time.deltaTime and still fit it into a byte?

I am confused about this issue, and I can't find why I get this and how to fix it properly from the Google searches I've tried.

This is a short version of my code:

     byte redtarget = 70;
byte greentarget = 222;
byte bluetarget = 255;
byte opacitytarget = 1;
byte redcurrent = 0;
byte greencurrent = 0;
byte bluecurrent = 0;
void OnMouseOver() {
redcurrent = redcurrent + redtarget * Time.deltaTime;
greencurrent = greencurrent + greentarget * Time.deltaTime;
bluecurrent = bluecurrent + bluetarget * Time.deltaTime;
if(redcurrent >= redtarget) {
redcurrent = redtarget;
}
if(greencurrent >= greentarget) {
greencurrent = greentarget;
}
if(bluecurrent >= bluetarget) {
bluecurrent = bluetarget;
}
theobject.GetComponent<Renderer>().material.SetColor("_EmissionColor",new Color32(redcurrent,greencurrent,bluecurrent,opacitytarget)) ;
}


Please ignore the other errors that could have been introduced when I copy + pasted it to here.

And this is the error I get:

As you see the error, "cannot implicit convert type float to byte", the problem is caused by Time.deltaTime that returns a float value, and I need a byte due to Color32.

Is there any way to turn Time.deltaTime to byte? Or should I use another type of color that use floating point numbers?

I'm a beginner with Unity so I'm sorry if this has already been asked somewhere; as I said, I can't find it on the Internet.

• As beginner, dont be scared to read the compiler messages. This one, for instance, tells you both what is wrong and how to fix it. Jan 9, 2017 at 16:30
• i know but i just ... cant ... understand it .... Jan 9, 2017 at 16:31
• A byte can hold integer values from 0 to 255. Time.deltaTime is a float, and it can hold much more different values. The compiler cannot implicitly convert from float to byte because you would lose a lot of data, so it asks you to explicitly do the casting (i.e. "tell me you're sure you want to do this"). In this case, that's not what you want. You want to change the type of redtarget and others to float, because you'll have to take the floating point values in consideration, whereas you could not with byte. Jan 9, 2017 at 16:31
• if i changed to float , i cant use color32 :\ Jan 9, 2017 at 16:32
• to be precise, use float for computation and cast the final results to byte when instantiating the color(since float color is from 0 to 1, while byte colors are from 0 to 255). This is what you probably intended, but might be problematic due to reasons described above. Jan 9, 2017 at 16:33

The error

Cannot implicitly convert type 'float' to 'byte'. An explicit conversion exists (are you missing a cast?)

means that you try to assign a value for a type to another where information could be lost (generally).

A byte can hold integer values from 0 to 255. Time.deltaTime is a float, and it can hold much more different values. The compiler cannot implicitly convert from float to byte because you would lose a lot of data, so it asks you to explicitly do the casting (i.e. "tell me you're sure you want to do this").

So to simply make the issue go away, you'd have to explicitly convert the Time.deltaTime to byte like this:

redcurrent = redcurrent + redtarget * (byte)Time.deltaTime;


However, in this case, that's not what you want. You want to change the type of redtarget and others to float, because you'll have to take the floating point values in consideration in this current situation, whereas you could not with byte.

Only doing this, you'll get more type conversion errors when trying to create a Color32 object.

To do that, you cast the values when/where you need them:

theobject.GetComponent<Renderer>().material.SetColor(
"_EmissionColor",
new Color32(
(float)redcurrent,
(float)greencurrent,
(float)bluecurrent,
opacitytarget)) ;


For something like this, I'd recommend using either the Color or Color32 types throughout.

eg.

Color32 currentColor;
Color32 originalColor;
Color32 targetColor;
float blendProgress;

Material myMaterial;

void OnMouseOver() {

This keeps your code cleaner, and more explicit about what it's trying to do. You can still access bytes for the red, green, blue, and alpha components via currentColor.r etc.