0
\$\begingroup\$

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:

what ?

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.

\$\endgroup\$
10
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – wondra
    Jan 9, 2017 at 16:30
  • \$\begingroup\$ i know but i just ... cant ... understand it .... \$\endgroup\$ Jan 9, 2017 at 16:31
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Vaillancourt
    Jan 9, 2017 at 16:31
  • \$\begingroup\$ if i changed to float , i cant use color32 :\ \$\endgroup\$ Jan 9, 2017 at 16:32
  • \$\begingroup\$ 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. \$\endgroup\$
    – wondra
    Jan 9, 2017 at 16:33

2 Answers 2

2
\$\begingroup\$

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)) ;
\$\endgroup\$
2
\$\begingroup\$

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() {
   // Advance your blend progress.
   blendProgress = Mathf.Clamp01(blendProgress + Time.deltaTime);

   // Blend smoothly between the original and destination colors.
   currentColor = Color32.Lerp(originalColor, targetColor, blendProgress);

   // Cache your material in Start so you don't need to look it up every frame.
   myMaterial.SetColor("_EmissionColor", currentColor);
}

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .