Unexpected symbol (544754)

Hi guys,
I’m using unity’s 2d features to make my game and i’m getting the cs1525 error, upon researching the error it seems that most of the time the error is due to miss-typing or the miss-placement of code, however I cant see any problems with my code.

using UnityEngine;
using System.Collections;

public class playerMove : MonoBehaviour {

    //declare the max speed
    public float maxSpeed= 4.5f;
    // the player character is facing to the right by default
    public bool facingRight= true;

    Animator anim;
  
    void Start ()
    {
        anim= GetComponent<Animator>();
    }

    void FixedUpdate ()
    {
        public float playerMove = Input.acceleration.x; //move equals acceleration.
        //used to be less than neg
        if(move < -0.2f || move > 0.2f) // if the device is tilted so far in either direction
        {

            anim.SetFloat ("Speed", Mathf.Abs(playerMove)); //set the Speed parameter to the value after the comma

            rigidbody2D.velocity= new Vector2 (playerMove * maxSpeed, rigidbody2D.velocity.y); //declare the x and y of the rigid body

            if(playerMove > 0 && !facingRight) // if move is more than 0 and the player is NOT facing right
                Flip(); //see flip below
            else if(playerMove < 0 && facingRight) //if move is less than 0 and facing right
                Flip(); //see flip below
            }

        else if(playerMove > -0.1f || playerMove < 0.2f)
        {
            anim.SetFloat ("Speed", 0);
            rigidbody2D.velocity= new Vector2 (0,0);
        }

    }

    void Flip()
    {
        facingRight= !facingRight;
        Vector3 theScale= transform.localScale;
        theScale.x *= -1;
        transform.localScale= theScale;
    }
}

the problem is with this particular line - public float playerMove = Input.acceleration.x; //move equals acceleration. (line 20)

It seems to have a problem with the public part before the float. can anyone see why this would be a problem?
I need to access this float from another separate script so I need this float to be public.

Cheers guys,

Mark

Remove the public. A local variable can’t have an accessibility modifier.
If you need the state held on to by the variable to be accessible in multiple places, you need to use a field or property on the class. Local variables only exist for the duration of a method execution.

If you need it accessible from another script simply put the variable outside FixedUpdate simply as:

public float playerMove;

then in FixedUpdate you need just to call:

playerMove = Input.acceleration.x;

Local variables inside functions can’t be public as Rene Damm said.

Ah okay, thanks guys.
I actually tried putting it outside the void update but I just transferred the whole line instead of applying the input.acceleration inside the void update.

Cheers :slight_smile:
Mark