Search Unity

Assets/BallControl.cs(11,2):error CS1525: Unexpected symbol 'if'

Discussion in 'Scripting' started by ThreeCents, Jul 12, 2018.

  1. ThreeCents

    ThreeCents

    Joined:
    Jul 12, 2018
    Posts:
    32
    I got another problem while scripting : Assets/BallControl.cs(11,2):error CS1525: Unexpected symbol 'if'
    I searched on the 11 line but I didn't find the problem.

    Here :

    void GoBall(){
    float rand = Random.Range(0, 2)
    if(rand < 1){
    rb2d.Addforce(new Vector2(20, -15));
    } else {
    rb2d.AddForce(new Vector2(-20, -15));
    }

    }
     
  2. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    use Code Tags, in the bar above with all the bold, italic, underline etc. there is a button code: <> use it :)
    it makes code a lot more readable. Try editting your main post and put the code into code tags.

    float rand = Random.Range(0, 2) <- is missing a semicolon ; again due to that it reads it as one line.
    "float rand = Random.Range(0, 2) if(rand < 1){" <- not a valid line

    Here:
    Code (CSharp):
    1. void GoBall()
    2. {
    3.     float rand = Random.Range(0, 2);
    4.     if (rand < 1)
    5.     {
    6.         rb2d.Addforce(new Vector2(20, -15));
    7.     }
    8.     else
    9.     {
    10.         rb2d.AddForce(new Vector2(-20, -15));
    11.     }
    12. }
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Many times with unexpected symbols, you need to look at the previous line for things just like this. Keep that in mind in the future with debugging.
     
  4. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    Also if you have Visual studio IDE it should also tell you while hovering over that it is missing a semicolon.
    it is red lined at the end, mostly meaning that something is wrong.

    Visual Studio.png
     
  5. ThreeCents

    ThreeCents

    Joined:
    Jul 12, 2018
    Posts:
    32
    Ok thanks ^^ !