An object reference is required error how can i fix it?

I get two errors saying that

“An object reference is required for the non-static field, method, or property ’ UnityEngine.Transform.position.get”


“An object reference is required for the non-static field, method, or property ’ UnityEngine.Transform.rotation.get”

The code is meant to make a bullet and shoot it forward. I’m new to C# so if its an epic fail don’t laugh and stuff :slight_smile:


using UnityEngine;
using System.Collections;

public class Breakthewall : MonoBehaviour {

	public Rigidbody bullet;
	public float power = 1500f;

	void Update () {
		if (Input.GetButtonUp ("Fire1")){

			Rigidbody instance = Instantiate(bullet, Transform.position, Transform.rotation) as Rigidbody;

			Vector3 fwd = transform.TransformDirection(Vector3.forward);

			instance.AddForce (fwd * power);

Listen very carefully, because this takes a bit of explaining. Transform is a class. The Transform class has some member functions that belong to all instances of Transform. The documentation describes these as “Static Functions”, see:

So, for example, you can call Transform.Destroy() to destroy things. A different example would be the GameObject.FindObjectOfType() call. In both examples you’re using the class name and calling a function on it. This is different from how you usually call functions, by giving an object to the left of the dot. Actually another super example is the Input.GetButtonUp() that you have in your code. GetButtonUp is a static member of Input.

So, if there is not a position member of the class Transform what is there? Well, there is a documented variable called position but it’s not a static member. Because it’s not a static member it can only be used on an object. So, if you have:

Transform fred;

later in your code you can do:


Fred is an instance of Transform so you can get it’s position.

Which brings me to your problem, which is you have a capital T in your code. A GameObject has a Transform member called transform (lower-case t). If you use the capital T then you mean the static member of the class. If you use the lower case t then you mean the variable called transform which just happens to be a Transform.