How to fix that compiler error? CS0236
So there's the error that I get: error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'
and there's my code:
using UnityEngine; using System.Collections;
public class Controle : MonoBehaviour {
Vector3 Transform = transform.position;
// Update is called once per frame
void Update () {
float MoveHorizontal = Input.GetAxis ("Horizontal");
Transform.x = Transform.x + MoveHorizontal;
}
}
I am really confused and don't understand what do i've done wrong... English isn't my first language so maybye it's something not hard to understand
Answer by tanoshimi · Oct 31, 2016 at 02:38 PM
transform.position is not a known value, so you can't use it to initialise a variable Transform. You can assign that value in Start(), say. Also, you shouldn't name variables with the same name as a class name. So tbis is better...
Vector3 myTransform;
void Start() {
myTransform = transform.position;
}
Your answer

Follow this Question
Related Questions
Bizarre compile errors in Unity while Visual Studio will build project successfully 0 Answers
Project file could not be loaded - Root element is missing from MonoDevelop 0 Answers
It works great on editor, but I need it to work on my build. (UnityEditor.TransformUtils) 3 Answers
Unable to load project - specified pathname not found compilation error 0 Answers
CS1023 An embedded statement may not be a declaration or labeled statement help 1 Answer