HELP Parser error unexpected symbol "if"

Hi there,

I am unable to continue as I keep getting this error “Parser error unexpected symbol “if””.
Assets/lePlayer.cs(13,64): error CS1525: Unexpected symbol `if’

This is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class lePlayer : MonoBehaviour {
private int playerSpeed = 4;
private int jumpheight = 1200;
public bool isGrounded = false;

void FixedUpdate()
{
isGrounded = Physics.Raycast(transform.position, -Vector3.up, if);
}

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

if (Input.GetKey (“right”))
{
transform.position -= Vector3.left * playerSpeed * Time.deltaTime;
}

if (Input.GetKey (“left”))
{
transform.position -= Vector3.right * playerSpeed * Time.deltaTime;
}

if (Input.GetKeyDown (“space”) && isGrounded)
{
GetComponent ().AddForce (new Vector3 (0, jumpheight, 0), ForceMode.Force);
}

}
}

maybe missing a “}” or have one too many

in your case maybe one too many remove the last “}”

I counted 7 openings and 8 closings, these should have a equal count open"{" and close"}".

Please use code tags next time.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class lePlayer : MonoBehaviour {
    private int playerSpeed = 4;
    private int jumpheight = 1200;
    public bool isGrounded = false;

    void FixedUpdate()
    {
        //what is this "if" doing here?       
        isGrounded = Physics.Raycast(transform.position, -Vector3.up, if);
    }

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        if (Input.GetKey ("right"))
        {
            transform.position -= Vector3.left * playerSpeed * Time.deltaTime;
        }


        if (Input.GetKey ("left"))
        {
            transform.position -= Vector3.right * playerSpeed * Time.deltaTime;
        }


        if (Input.GetKeyDown ("space") && isGrounded)
        {
            GetComponent<Rigidbody> ().AddForce (new Vector3 (0, jumpheight, 0), ForceMode.Force);
        }


        }
    }
}

Still a offset with open"{“(7) and close”}"(8), they should be equal.

hence…
open"{“(7) and close”}"(7), - equal

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
  
    public class lePlayer : MonoBehaviour {
        private int playerSpeed = 4;
        private int jumpheight = 1200;
        public bool isGrounded = false;
  
        void FixedUpdate()
        {
            //what is this "if" doing here?   
            isGrounded = Physics.Raycast(transform.position, -Vector3.up, if);
        }
  
        // Use this for initialization
        void Start () {
  
        }
  
        // Update is called once per frame
        void Update () {
  
            if (Input.GetKey ("right"))
            {
                transform.position -= Vector3.left * playerSpeed * Time.deltaTime;
            }
  
  
            if (Input.GetKey ("left"))
            {
                transform.position -= Vector3.right * playerSpeed * Time.deltaTime;
            }
  
  
            if (Input.GetKeyDown ("space") && isGrounded)
            {
                GetComponent<Rigidbody> ().AddForce (new Vector3 (0, jumpheight, 0), ForceMode.Force);
            }
  
  
            }
        }

Hi there,

I have tried your code and it is still not working. I am getting the same error message. I am simply trying to follow this Youtube 2d game video . Please skip to 5:37 where he does the code. I’m not sure whats going on…please help.
(
https://www.youtube.com/watch?v=dt16kqAUFuw
)

I believe he was just telling you to ad code when inserting here in the forums to make it more easy to try and detect where the problem resides. the fix is in the code I’ve inserted again below.

Still a offset with open"{“(7) and close”}"(8), they should be equal.

hence…
open"{“(7) and close”}"(7), - equal

 using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class lePlayer : MonoBehaviour {
        private int playerSpeed = 4;
        private int jumpheight = 1200;
        public bool isGrounded = false;
        void FixedUpdate()
        {
            //what is this "if" doing here? 
            isGrounded = Physics.Raycast(transform.position, -Vector3.up, if);
        }
        // Use this for initialization
        void Start () {
        }
        // Update is called once per frame
        void Update () {
            if (Input.GetKey ("right"))
            {
                transform.position -= Vector3.left * playerSpeed * Time.deltaTime;
            }
            if (Input.GetKey ("left"))
            {
                transform.position -= Vector3.right * playerSpeed * Time.deltaTime;
            }
            if (Input.GetKeyDown ("space") && isGrounded)
            {
                GetComponent<Rigidbody> ().AddForce (new Vector3 (0, jumpheight, 0), ForceMode.Force);
            }
            }
        }

Which one should I get rid of? I am quite confused as I tried counting and got lost track…

Copy paste the code I just posted!

Dude stop.
The error was unexpected “if” keyword, not “missing }”. So he probably just didn’t scroll to the end of document when he copy-pasted the code here.
I even pointed out it’s location, passing “if” keyword as 3rd argument in Physics.Raycast().
You even copied modified version and didn’t read it. TWICE NOW.

COPY POSTED :smile:

Because I didn’t change anything other than adding a comment in void FixedUpdate();
You ALSO copied and didn’t read it.
You’re passing “if” keyword as third argument in Physics.Raycast when it should be “1f”.

void FixedUpdate()
        {
            //what is this "if" doing here?
            isGrounded = Physics.Raycast(transform.position, -Vector3.up, if);
        }

Should be:

void FixedUpdate()
        {
            //what is this "if" doing here?
            isGrounded = Physics.Raycast(transform.position, -Vector3.up, 1f);
        }
1 Like
 using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class lePlayer : MonoBehaviour {
        private int playerSpeed = 4;
        private int jumpheight = 1200;
        public bool isGrounded = false;
        void FixedUpdate()
        {
            //what is this "if" doing here? 
            isGrounded = Physics.Raycast(transform.position, -Vector3.up, if);
        }
        // Use this for initialization
        void Start () {
        }
        // Update is called once per frame
        void Update () {
            if (Input.GetKey ("right"))
            {
                transform.position -= Vector3.left * playerSpeed * Time.deltaTime;
            }
            if (Input.GetKey ("left"))
            {
                transform.position -= Vector3.right * playerSpeed * Time.deltaTime;
            }
            if (Input.GetKeyDown ("space") && isGrounded)
            {
                GetComponent<Rigidbody> ().AddForce (new Vector3 (0, jumpheight, 0), ForceMode.Force);
            }
            }
        }

I tried using your code but it didn’t work. Still error message and getting
Assets/lePlayer.cs(13,64): error CS1525: Unexpected symbol `if’

Thanks for the reply. Same error but with this message bottom left hand corner "
Assets/lePlayer.cs(11,64): error CS1525: Unexpected symbol `if’
"

Sorry if I triggered you but I’m quite new to this code thing. Please go easy on me.

1 Like

Problem Solved :smile:. Thanks so much for your explanation :smile:

1 Like

That was a reply for the @TSC spammer in this thread that copy pasted the same code 3 times now, not you. You’re fine. Just do what I wrote in the previus post.

because that’s a different problem it tells you where the problem is line 11

isGrounded = Physics.Raycast(transform.position, -Vector3.up, if);

should be
isGrounded = Physics.Raycast(transform.position, -Vector3.up);

Glad you solved it.

1 Like

Wrong. Check the video. Stop shotgun posting.

Hold on @FMark92 he did have a offset then when it was fixed he got a different error even he said it say this now… how am I spamming? I’m trying to help? But ok.