Cannot Implicitly Convert Type HELP

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 :frowning:

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?

The line

if (Up()) {

evaluates if the return value of Up() is true or false.
The line

public void Up() {

defines the return type void (no return value) for you Up function.

I am calling it through a button. So when I hold the UI button down, it will continue to spin. But it does not do that

Then you don’t need to place Up() under update. Just call the “up” directly from the button. No need for an if statement.

I understand that, however, when I hold the button down, it does not continue to apply torque at the rate that it shows above.

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.

Great :slight_smile:

1 Like