- Home /
an object reference is required to access non-static member
I know this is a frequent issue, but here's the code:
using UnityEngine;
using System.Collections;
public class CameraMovement : MonoBehaviour {
int CameraMovementSpeed = 2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Transform.Translate (0, 0, CameraMovementSpeed);
}
}
I'm new to Unity and C#, in fact my only experience is in basic C++ programming. I have two questions:
Why isn't this working? I'm trying to mimic this javascript version: http://youtu.be/K81JJdA2lnk?t=10m34s
Why does it all have to be in class brackets? Can't it look like this instead? (Here it even complains about the "int CameraMovementSpeed = 2;")
using UnityEngine; using System.Collections;
int CameraMovementSpeed = 2; // Update is called once per frame void Update () { Transform.Translate (0,0,CameraMovementSpeed); }
Answer by robertbu · Jun 14, 2014 at 02:58 PM
You must have the class declaration in C#. Javascripts are also a class derived from Monobehavior, but it is done under the hood rather than explicitly.
Your problem is on line 13. Upper case 'Transform' should be 'transform'. 'Transform' is the class. 'transform' is the specific transform of the game object.
Changing it to "transform" doesn't remove the error message.
EDIT: It seems that though the error message is still there, the camera moves as it's intended to. Thanks!
Your answer
Follow this Question
Related Questions
Script activate after amount of time 3 Answers
Distribute terrain in zones 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
TextMesh isn't updating itself. 0 Answers
Multiple Cars not working 1 Answer