Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with using Public float

Discussion in 'Getting Started' started by Doriangay, Mar 20, 2016.

  1. Doriangay

    Doriangay

    Joined:
    Mar 20, 2016
    Posts:
    2
    I'm currently following a tutorial (outdated, which is why I'm not just copy/pasting) and Unity is telling me:

    "Assets/Scripts/PlayerMover.cs(8,20): error CS1519: Unexpected symbol `float' in class, struct, or interface member declaration"

    Here is the code


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMover : MonoBehaviour {
    5.  
    6.     private Rigidbody
    7.  
    8.     public float speed = 30;
    9.  
    10.     void Start ()
    11.     {
    12.         rb = GetComponent<Rigidbody> ();
    13.     }
    14.  
    15.     void FixedUpdate ()
    16.     {
    17.         float v = Input.GetAxisRaw ("Vertical");
    18.         rb.Velocity = new Vector2 (0.0f, v) * speed;
    19.     }
    20. }
    21.  
    The "float" it seems to have an issue with is the "public float speed = 30;" one.

    ( have searched the forums and online but I can't find a solution to this specific issue, sorry)
     
  2. mholub

    mholub

    Joined:
    Oct 3, 2012
    Posts:
    123
    It is because on previous line you have "private Rigidbody", which is unfinished declaration. It should be
    "private Rigidbody rb;"
     
    JoeStrout likes this.
  3. Doriangay

    Doriangay

    Joined:
    Mar 20, 2016
    Posts:
    2
    Just my luck it would be something that simple. Thank you so much for your help :)
     
  4. Freakyuno

    Freakyuno

    Joined:
    Jul 22, 2013
    Posts:
    138
    Also, for the correctness of C#, you should define your float like this.

    Code (CSharp):
    1. public float speed = 30.0f
    Technically the way you're doing it now uses the built in implicit operator from integer to float (also read as a conversion or "cast" in c#). There's overhead to any cast, though in this case it's extremely small.
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    There is no overhead to the cast (implicit or otherwise) of a numeric literal. It's done at compile time, not at runtime, and the work done is no more than the work it would take to compile the float literal (in fact, it's probably infinitesimally less work, since the int literal is three characters shorter).

    I generally leave the ".0f" off in cases like these, to make the code shorter and more readable.

    I also remember to put a semicolon on the end of the line. ;)
     
    Kiwasi likes this.
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Many errors are all to do with the line immediately prior to the one that throws the error. Always check the line before.
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,141
    Wait, Unity doesn't yell at you whenever you try to leave off the "f" on floating point numbers? :eek:
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    No, not if you also leave off the decimal point (and whatever comes after). Then it's an int literal, and you can assign an int literal to a float lvalue just fine.
     
  9. Freakyuno

    Freakyuno

    Joined:
    Jul 22, 2013
    Posts:
    138
    Thats only true if it's a compile time constant. You'd be correct if it was marked const or readonly but it's not, which means it can't handle that at compile time. Look at the IL that's generated.

    Take the following example.
    float x;
    float y = x * 20.0;

    x in this case will actually be promoted to a double at compile time, not a float at all, and your precision will be wrong in future calculations.

    If you wrote it this way
    float x;
    float y = x * 2;

    x will be promoted to a float from integer, and by luck you'll get the correct precision because of the promotion properties.

    Take a quick look at this link. You can get some really unusual results by not being explicit with your numeric castings.

    https://msdn.microsoft.com/en-us/library/3t4w2bkb(v=vs.80).aspx
     
  10. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    Unity assumes something like 5.0 is a double, which cannot be implicitly cast to a float.
     
  11. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Which it is. Remember, the line under discussion was:

    Code (csharp):
    1. public float speed = 30;
    30 here is a constant expression. It could be 10 * 60 / 2, and it would still be compiled to the exact same thing.

    I'm correct regardless. The attributes of the LHS have nothing to do with constant-folding in the RHS. (Except, of course, for the automatic type conversion which can be folded in as well.)

    Your examples, involving a variable expression, are a completely different case from what we were talking about.

    Best,
    - Joe
     
    Deleted User likes this.