Question

I'm trying to create a 2D game in Unity 3D, but I'm getting the following error:

Parse error: Only assignments, call, increment, decrement, await, and new object expressions can be used as a statement

I've followed the guide but I'm still getting the error.

My code:

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {

public float maxSpeed = 10f;
bool facingRight = true;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void FixedUpdate () {
    float move = Input.GetAxis ("Horizontal");
    rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
    if(move > 0 &&! facingRight){
        Flip ();
    }
    else if(move < 0 && facingRight){
        Flip ();
    }
}
void Flip () {
    facingRight != facingRight;
    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;
}
    }
Was it helpful?

Solution

That is caused by the following statement

facingRight != facingRight;

!= is an inequality operator. It returns true if both sides are unequal, and false if both sides are equal. Its counterpart would be the == operator.

What you most likely meant to do there is

facingRight = !facingRight;

This sets facingRight to be whatever is on the right-hand side. Which is !facingRight, which of course causes facingRight to become true if it is currently false, or false if it's currently true.

P.s. you generally might want to include on which line the error occurs. Unity should tell you about that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top