Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Parser Error Please Help. (Fixed)

Discussion in 'Scripting' started by Ducraw, Mar 6, 2015.

  1. Ducraw

    Ducraw

    Joined:
    Mar 6, 2015
    Posts:
    8
    I'm a beginner with unity.
    I have this code and I see the error but I don't know how to fix it? Please help me.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Playercontrol : MonoBehaviour {
    5.  
    6.     public float maxspeed = 10f;
    7.     bool facingRight = true;
    8.    
    9.     void Start () {}
    10.  
    11.     void FixedUpdate ()
    12.     {float move = Input.GetAxis (ˮHorizontalˮ);
    13.         Rigidbody2D.velocity = new Vector2(move * maxspeed, Rigidbody2D.velocity.y ;
    14.  
    15.             Flip ();
    16.  
    17.            else if (move < 0 && facingRight)
    18.             Flip ();
    19.     }
    20.     void Flip()
    21.     {
    22.         facingRight = !facingRight;
    23.         Vector3 theScale = transform.localScale;
    24.         theScale.x *= -1
    25.  
    26.         transform.localScale = theScale;
    27.  
    28.        
    29.     }
    30. }
    31.  
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    There is no beginning if to start your else if off for a start. Looks like you missed a line at line 14. Try adding this:

    if (move >0 && !facingRight)
     
  3. Ducraw

    Ducraw

    Joined:
    Mar 6, 2015
    Posts:
    8
    Thank, but I still have some error.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Which line is the error pointing to?

    In general parsing error means the compiler has given up trying to figure our what you told it. Its often caused by miss matched brackets.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Playercontrol : MonoBehaviour {
    5.  
    6.     public float maxspeed = 10f;
    7.     bool facingRight = true;
    8.    
    9.     void FixedUpdate () {
    10.         float move = Input.GetAxis (ˮHorizontalˮ);
    11.         Rigidbody2D.velocity = new Vector2(move * maxspeed, Rigidbody2D.velocity.y ;
    12.         if (move > 0 && !facingRight){
    13.             Flip ();
    14.         } else if (move < 0 && facingRight) {
    15.             Flip ();
    16.         }
    17.     }
    18.  
    19.     void Flip()
    20.     {
    21.         facingRight = !facingRight;
    22.         Vector3 theScale = transform.localScale;
    23.         theScale.x *= -1;
    24.         transform.localScale = theScale;    
    25.     }
    26. }
    27.  
    Copy this across. It should compile now.
     
  6. Ducraw

    Ducraw

    Joined:
    Mar 6, 2015
    Posts:
    8
    I don't see the parser error anymore but it keep telling me to fix the error in unity.
     
  7. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Change line 11 in the updated version from this:
    Code (csharp):
    1.  
    2. Rigidbody2D.velocity = new Vector2(move * maxspeed, Rigidbody2D.velocity.y ;
    3.  
    to this
    Code (csharp):
    1.  
    2. Rigidbody2D.velocity = new Vector2(move * maxspeed, Rigidbody2D.velocity.y);
    3.  
    Missing an ending parenthesis.
     
  8. Ducraw

    Ducraw

    Joined:
    Mar 6, 2015
    Posts:
    8
    Thank, but it still giving me error.
     
  9. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    What's the error?
     
  10. Ducraw

    Ducraw

    Joined:
    Mar 6, 2015
    Posts:
    8
    It keep saying that I need to fix all the error before entering playmode
     
  11. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Yes, but what are the errors. Are they not in the console window?
     
  12. Ducraw

    Ducraw

    Joined:
    Mar 6, 2015
    Posts:
    8
    It said at the bottom of unity that
    assets/code/Player control.cs(10,45): error CS0103: The name '?Horizontal?' does not exist in the current context
     
  13. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Line 10, Column 45... where it says "Horizontal" is where the issue is. Is it not enclosed in quotes?
     
  14. Ducraw

    Ducraw

    Joined:
    Mar 6, 2015
    Posts:
    8
    So how do I fix it?
     
  15. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    1) Get rid of the quotes that were pasted in and retype them (before the H in Horizontal and after the l in Horizontal). There is something funky about those quotes.

    2) I caught this error because I took the time to copy the code into Visual Studio - you are attempting to call a non-static function as if it were static (RigidBody2D).

    Here is the code, fixed up. You should be able to copy and paste this. Let me know if you run into any other issues.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Playercontrol : MonoBehaviour
    5. {
    6.     public float maxspeed = 10f;
    7.     bool facingRight = true;
    8.  
    9.     void FixedUpdate() {
    10.         float move = Input.GetAxis ("Horizontal");
    11.  
    12.         Rigidbody2D rigidBody2D = GetComponent<Rigidbody2D>();
    13.         if (rigidBody2D != null)
    14.         {
    15.            rigidBody2D.velocity = new Vector2(move * maxspeed, rigidBody2D.velocity.y);
    16.         }
    17.  
    18.         if (move > 0 && !facingRight){
    19.             Flip ();
    20.         } else if (move < 0 && facingRight) {
    21.             Flip ();
    22.         }
    23.     }
    24.  
    25.     void Flip()
    26.     {
    27.         facingRight = !facingRight;
    28.         Vector3 theScale = transform.localScale;
    29.         theScale.x *= -1;
    30.         transform.localScale = theScale;
    31.     }
    32. }
     
    eelstork likes this.
  16. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    Copy-paste-from-web error.

    Stare VERY intently at these double quotes in the code you just pasted, you will notice they are a bit slanted to the right:
    Not the one you want => ˮ
    The one you want => "

    So you go and edit your code and type the " key on your kbd to replace these slanted double quotes around "Horizontal".

    Take your time and don't copy and paste too much code. Although for some reason you will always find people to flag basic errors, it won't take you anywhere since it takes too long to get help this way.

    Welcome to the World of Programming.
     
    Ducraw and knr_ like this.
  17. Ducraw

    Ducraw

    Joined:
    Mar 6, 2015
    Posts:
    8
    Thank, it working now