Search Unity

Cannot Implicitly Convert Type HELP

Discussion in 'Scripting' started by Tritus, Aug 30, 2015.

  1. Tritus

    Tritus

    Joined:
    Aug 30, 2015
    Posts:
    4
    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:

    Code (csharp):
    1.  
    2. public int force;
    3. Rigidbody rigidBody;
    4. public Button boost;
    5. //Transform capsule;
    6. void Start () {
    7.  
    8. rigidBody = this.GetComponent<Rigidbody>();
    9. //capsule = this.GetComponent<Transform> ();
    10.  
    11. boost = GetComponent<Button> ();
    12.  
    13.  
    14.  }
    15.  
    16. //Updateiscalledonceperframe
    17. void Update () {
    18. if (Up()) {
    19. Up ();
    20.  }
    21.  
    22.  }
    23.  
    24.  
    25. public void Boost()
    26.  {
    27. rigidBody.AddForce(transform.forward * force);
    28.  }
    29.  
    30. public void Up() {
    31. rigidBody.AddTorque(0,0,1f);
    32.  }
    33.  
    34.  
    35. publicvoidDown() {
    36. rigidBody.AddTorque(0,0, -1f);
    37.  }
    38.  
    39. }
     
  2. Rob-Howard

    Rob-Howard

    Joined:
    Sep 4, 2014
    Posts:
    9
    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?
     
  3. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    The line
    Code (CSharp):
    1. if (Up()) {
    evaluates if the return value of Up() is true or false.
    The line
    Code (CSharp):
    1. public void Up() {
    defines the return type void (no return value) for you Up function.
     
  4. Tritus

    Tritus

    Joined:
    Aug 30, 2015
    Posts:
    4
    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
     
  5. Rob-Howard

    Rob-Howard

    Joined:
    Sep 4, 2014
    Posts:
    9
    Then you don't need to place Up() under update. Just call the "up" directly from the button. No need for an if statement.
     
  6. Tritus

    Tritus

    Joined:
    Aug 30, 2015
    Posts:
    4
    I understand that, however, when I hold the button down, it does not continue to apply torque at the rate that it shows above.
     
  7. Rob-Howard

    Rob-Howard

    Joined:
    Sep 4, 2014
    Posts:
    9
    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.
     
  8. Tritus

    Tritus

    Joined:
    Aug 30, 2015
    Posts:
    4
    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.
     
  9. Rob-Howard

    Rob-Howard

    Joined:
    Sep 4, 2014
    Posts:
    9
    Great :)
     
    Tritus likes this.