Hi I am new to Unity and I are building a racing car game This is my problem: error CS0023: The `! ' operator can not be applied to operand of type `float ' Please guide me how can I fix this My problem on line 11
public WheelCollider W_FL;
public WheelCollider W_FR;
public WheelCollider W_RL;
public WheelCollider W_RR;
private float V;
private float H;
private int M_Speed = 30;
private int T_Speed = 150;
public float C_Speed;
void FixedUpdate () {
if (!V)
{
W_RL.brakeTorque = 100;
W_RR.brakeTorque = 100;
}
else {
W_RL.brakeTorque = 0;
W_RR.brakeTorque = 0;
}
C_Speed = 2 * 3.14f * W_RL.radius * W_RL.rpm * 60 / 1000;
C_Speed = Mathf.Round (C_Speed);
if (C_Speed < T_Speed && C_Speed > -30) {
W_RL.motorTorque = M_Speed * V;
W_RR.motorTorque = M_Speed * V;
}
else {
W_RL.motorTorque = 0;
W_RR.motorTorque = 0;
}
}
void Update () {
V = Input.GetAxis("Vertical");
}
Answer by Commoble · Mar 04, 2017 at 07:05 PM
The error says that you cannot apply the ! operator to a variable of type float. You have if (!V)
, where you are applying the ! operator to the variable V, which you earlier declared as a variable of type float, which you can't do, as the error message tells you. Floats have a complicated binary representation and can't be interpreted as a boolean value, and ! can only apply to bools or types that can be interpreted as bools.
Judging from the rest of your code, you want if (V == 0F)
or something similar.
Answer by AMU4u · Mar 04, 2017 at 07:00 PM
You cannot apply the operator like that. You are saying, "If V is Not....." because V is a float. It has no parameters beyond it's value. The operator you are using is commonly used for bools.
Your answer
Follow this Question
Related Questions
Stop Unity from opening blender. 0 Answers
WHAT THE HECK? Rotations not working. 0 Answers
My cube gets stuck at 0 degrees rotation 1 Answer
trying to start making a fighting game... Where to start? 2 Answers