- Home /
C# Casting Compile Error
The motorTorque requires type float yet all my variables are of type double. I get the following error message starting at the code I provided below:
error CS0266: Cannot implicitly convert type
double' to
float'. An explicit conversion exists (are you missing a cast?)
ColliderBR.motorTorque = (float)ENGINE_POWER*motor;
ColliderBL.motorTorque = (float)ENGINE_POWER*motor;
//brake power to wheel brake
ColliderBR.motorTorque = (float)BRAKE_POWER*brake;
ColliderBL.motorTorque = (float)BRAKE_POWER*brake;
//steering wheel to steer the wheel
ColliderFL.steerAngle = (float)steer*MAX_STEER*steer_factor*Time.deltaTime*STEER_TIME;
ColliderFR.steerAngle = (float)steer*MAX_STEER*steer_factor*Time.deltaTime*STEER_TIME;
Comment
Answer by Statement · May 15, 2011 at 07:54 AM
You could try wrap all the expression in parenthesis, like this:
ColliderBR.motorTorque = (float)(ENGINE_POWER * motor);
ColliderBL.motorTorque = (float)(ENGINE_POWER * motor);
ColliderBR.motorTorque = (float)(BRAKE_POWER * brake);
ColliderBL.motorTorque = (float)(BRAKE_POWER * brake);
ColliderFL.steerAngle = (float)(steer * MAX_STEER * steer_factor * Time.deltaTime * STEER_TIME);
ColliderFR.steerAngle = (float)(steer * MAX_STEER * steer_factor * Time.deltaTime * STEER_TIME);
I think the reason you're getting this is that there is some variable(s) that are doubles, not floats, not necessarily just the first variables. Or change all your variables to floats to begin with, if plausible.