В чём проблема?
Вроде бы всё правильно,но оно мне пишет
Assets/Scripts/moving.cs(9,23): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `moving.obj'
и
Assets/Scripts/moving.cs(8,23): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `moving.obj'
ошибка,та же что там,что там.
Bот мой код:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moving : MonoBehaviour {
public GameObject obj;
private float speed = 5f;
private float xPos = obj.transform.position.x;
private float yPos = obj.transform.position.y;
private void Update () {
if (Input.GetKey (KeyCode.D)) {
xPos = xPos + speed * Time.deltaTime;
} else if (Input.GetKey (KeyCode.W)) {
yPos = yPos + speed * Time.deltaTime;
} else if (Input.GetKey (KeyCode.A)) {
xPos = xPos - speed * Time.deltaTime;
} else if (Input.GetKey (KeyCode.S)) {
yPos = yPos - speed * Time.deltaTime;
}
obj.transform.position = new Vector3 (xPos, yPos,0);
}
}