В чём ошибка Unity(CS0236)?

В чём проблема?
Вроде бы всё правильно,но оно мне пишет
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);
	}
}
  • Вопрос задан
  • 898 просмотров
Решения вопроса 1
TheTalion
@TheTalion
Описание ошибки: https://msdn.microsoft.com/en-us/library/5724t6za.aspx
Можно сделать так:
private float xPos;
private float yPos;

void Awake()
{
xPos = obj.transform.position.x;
yPos = obj.transform.position.y;
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы