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.
byte
can hold integer values from 0 to 255.Time.deltaTime
is afloat
, and it can hold much more different values. The compiler cannot implicitly convert fromfloat
tobyte
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 ofredtarget
and others tofloat
, because you'll have to take the floating point values in consideration, whereas you could not withbyte
. \$\endgroup\$