- Home /
Error CS0120, how to add an object reference?
I'm following a tutorial, and I've run into a problem, I'll just get this error twice: Assets/Code/Scripts/Spinner.cs(13,27): error CS0120: An object reference is required to access non-static member `UnityEngine.Transform.Rotate(UnityEngine.Vector3)' (the second error applies to line 13)
Note that this is my first question on the forum, so I'm not to sure what information might be relevant, so I'll just copy paste all my code (which luckely isn't that much).
Spinner.cs:
using UnityEngine;
using System.Collections;
public class Spinner : MonoBehaviour {
public void SpinLeft()
{
Transform.Rotate (0, 0, 60 * Time.deltaTime);
}
public void SpinRight()
{
Transform.Rotate(0, 0, -60 * Time.deltaTime);
}
}
LearningScript.cs:
using UnityEngine;
using System.Collections;
public class LearningScript : MonoBehaviour
{
GameObject capsuleGO;
Spinner cubeComp;
void start()
{
capsuleGO = GameObject.Find ("Capsule");
Debug.Log (capsuleGO);
cubeComp = GameObject.Find ("Cube").GetComponent<Spinner>();
Debug.Log(cubeComp);
}
void Update()
{
if (Input.GetKey (KeyCode.LeftArrow))
{
capsuleGO.GetComponent<Spinner> ().SpinLeft ();
}
if(Input.GetKey (KeyCode.RightArrow))
{
capsuleGO.GetComponent<Spinner>().SpinRight ();
}
if(Input.GetKey (KeyCode.UpArrow))
{
cubeComp.SpinLeft();
}
if(Input.GetKey (KeyCode.DownArrow))
{
cubeComp.SpinRight();
}
}
}
Also when I try to attach the LearningScript to the main camera, an error will contnue to pop up saying that the script isn't done compiling. Is this a related problem?
Any help is appreciated
Answer by rutter · Jan 03, 2014 at 12:00 AM
Welcome! As in your other question, it looks like case-sensitivity is an issue.
If you're familiar with the basics of object-oriented programming, you probably know that a "class" is a sort of blueprint for an "object". Most data and functions are associated with a particular object ("instance members"), but others are global to the class as a whole ("static members").
It just so happens that Transform
and transform
both have meaning:
transform
is the transform component for this particular GameObject (instance)Transform
is the transform class as a whole (static)
In a nutshell, this line and its sibling:
Transform.Rotate (0, 0, 60 * Time.deltaTime);
Should probably read closer to:
transform.Rotate (0, 0, 60 * Time.deltaTime);
After spending way to long on Google, I found this excact solution mere miutes before your reply. Just glad I've got the script finally working.
Thanks for the help!