Search Unity

Wierd error about conversion while scripting

Discussion in 'Scripting' started by RevolutionNow, Oct 12, 2014.

  1. RevolutionNow

    RevolutionNow

    Joined:
    Oct 12, 2013
    Posts:
    8
    Hi I was working on my script when a message popped up in monodevelop saying something about conversion, I clicked convert ( not really knowing what it meant). Now i am getting a wierd error about converting void to float.

    pleas help!
     
  2. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    Post the code where error occures and the message exactly.
     
  3. RevolutionNow

    RevolutionNow

    Joined:
    Oct 12, 2013
    Posts:
    8
    Code (JavaScript):
    1. var turn : float;
    2. var topSpeed : float = 10;
    3. var Acelleration : float = 5;
    4. var TurnSpeed : float = 5;
    5. var Friction : float;
    6. var Wheels : Component[];
    7. var WheelSize : float;
    8.  
    9. function Update () {
    10. Wheels = GetComponentsInChildren(WheelCollider);
    11. WheelSize = Wheels.Radius;
    12. Friction =
    13. TurnSpeed =
    14. topSpeed =
    15.  
    16. rigidbody.AddRelativeForce(0,0,Input.GetAxis("Vertical")* Acelleration);
    17.  
    18. turn += Input.GetAxis("Horizontal")* TurnSpeed;
    19.  
    20. transform.rotation=Quaternion.Euler(0,turn,0);
    21.  
    22. }
    the error message says " cannot convert void to float" and referencing to the place 28 in the 20th line.
     
  4. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    I don't know UnityScript but you assign turn variable vale of turn plus Input.GetAxis("Horizontal")* TurnSpeed where turn has not been assigned any value previously so perhaps the effect is void + something = void and thus later passing the turn variable value of void to Euler method produces the error since it expects floats not void.
     
  5. RevolutionNow

    RevolutionNow

    Joined:
    Oct 12, 2013
    Posts:
    8
    I tried assigning the turn variable but nothing changed. It is definately the 20th line that is giving the error
     
  6. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    The information about error message you provided is false. The error does not occur in line 20, it occurs in line 16.

    Nothing strage about the error. You're assigning Friction variable value of TurnSpeed which you assign value of topSpeed which you assign value of what rigidbody.AddRelativeForce method returns. If you look at the doc: http://docs.unity3d.com/ScriptReference/Rigidbody.AddRelativeForce.html it states it returns void. Since you decalred your variables Friction, TurnSpeed and topSpeed as float type you are trying to assign void value to float type variables and void cannot be converted into float since it makes no sens and that's exactly what the error message is saying.
     
    RevolutionNow likes this.
  7. RevolutionNow

    RevolutionNow

    Joined:
    Oct 12, 2013
    Posts:
    8
    Oh I se the problem now it was a very simple mistake. Thanks alot for the help :)