Hey guys,
I have this script and it keeps giving me the error that it cannot implicitly convert type ‘void’ to ‘bool’
I have tried everything and I cannot figure it out
Here is my code:
public int force;
Rigidbody rigidBody;
public Button boost;
//Transform capsule;
void Start () {
rigidBody = this.GetComponent<Rigidbody>();
//capsule = this.GetComponent<Transform> ();
boost = GetComponent<Button> ();
}
//Updateiscalledonceperframe
void Update () {
if (Up()) {
Up ();
}
}
public void Boost()
{
rigidBody.AddForce(transform.forward * force);
}
public void Up() {
rigidBody.AddTorque(0,0,1f);
}
publicvoidDown() {
rigidBody.AddTorque(0,0, -1f);
}
}
You have Up() as a function to return void. If you are wanting to apply constant up pressure (rigidbody.addtorque), you don’t need the “if” statement on line 18.
Though looking at your script, it looks like this is for a player controller, yes? If so, you’ll want to rework it. Are you wanting to check to see if a player hits a button like up arrow or “W” then apply force?
Ah, I see. Are you using a UI button? I think Unity’s UI buttons are either “clicked” or “not clicked”. I don’t think there is a “held” option for them, though I could be wrong. You’ll need to write some custom logic to deal with that.
Figured it out, I created a private bool for up. And when the PointerDown function is used, i set the up value equal to true. When the up value is true I set the Torque. Works great.