0

So, I'm playing around with some C# code in Unity for the first time. I'm creating a little script just to dink around with, not for actual use. This is the error I get:

Only assignment, call, increment, decrement, and new object expressions can be used as a statement

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {

    public int speed = 10;
    public int money = 10;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if(Input.GetKey(KeyCode.W)) {
            transform.Translate (Vector3.forward * speed * Time.deltaTime);
        }
        if(Input.GetKey(KeyCode.A)) {
            transform.Translate (Vector3.left * speed * Time.deltaTime);
        }
        if(Input.GetKey(KeyCode.S)) {
            transform.Translate (Vector3.back * speed * Time.deltaTime);
        }
        if(Input.GetKey(KeyCode.D)) {
            transform.Translate (Vector3.right * speed * Time.deltaTime);
        }
        if(Input.GetKey(KeyCode.Return && money >= 10)) {
            Debug.Log("You bought a sword!");
            money - 10;
        } else {
            Debug.Log("You don't have enough money!");
        }
    }
}

The error I get is in the title. It won't even let me run it. Does anyone know what I did wrong? Thanks for any help : )

1 Answer 1

5

It looks like you want to do

money -= 10;

not

money - 10;

The first is a decrement so it is allowed where as the second is just an expression (represents a value).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.