Object reference not set to an instance of an object.

Here’s dacloo again, not giving support a rest :smile:

I have a question, because I’m trying to figure out what is happening
for hours:

	cam = Camera.main.transform;
	cameraRelativeRight = cam.TransformDirection (1, 0, 0);

This is inside an update(), the script is tied to a cube,
and works. Except these above lines.

The error: Object reference not set to an instance of an object.
Huh? But I the script is tied to a cube, and I am telling which camera, right?

Thanks

var cam = Camera.main.transform; 
var cameraRelativeRight = cam.TransformDirection (Vector3(1, 0, 0));

Is your camera tagged as mainCamera? You can set and check the tag from the topmost inspector rollout in full mode.

thanks! Yes, it is tagged as the main camera.
But it still doesnt work.

This is the script:

var speed = 5;


function Update () {
	var cam = Camera.main.transform;
	var cameraRelativeRight = cam.TransformDirection (Vector3(1, 0, 0)); 

	if (Input.GetKey ("up")) {
   	     transform.Translate((5 * Vector3.forward) * Time.deltaTime);
    	}
	if (Input.GetKey ("down")) {
   	     transform.Translate((5 * Vector3.back) * Time.deltaTime);
    	}
	if (Input.GetKey ("right")) {
   	     transform.Translate((5 * cameraRelativeRight) * Time.deltaTime);
    	}
	if (Input.GetKey ("left")) {
   	     transform.Translate((5 * Vector3.left) * Time.deltaTime);
    	}
	}
	
function FixedUpdate () {
	if (Input.GetKey ("space")) {
		 rigidbody.AddForce (Vector3.up * 30);
		 rigidbody.AddTorque (Vector3.up * 10);
    	}
}

Can I also refer to a camera by name this way?
E.g: Camera.myFunnyCoolCamera.transform;

ODD BEHAVIOUR!

I re-loaded the project, and now it works! Without any problems. Nothing I have changed.

Still, the last question; can I reference any camera (or any object) by name? Or do I need to use a findByName(“name”) kind of construction?

The script works for me.

Try it in a new scene.
Make sure you have a rigidbody attached to the cube.

No. This is a special case we added as most games have a notion of ‘main camera’.

to get any object by tag, use FindGameObjectWithTag or FindGameObjectsWithTag

See: On The Edge - Det bedste fra tech verdenen

Thanks!

I have uploaded a pathetic little demo:
http://www.stickystudios.com/etc/test.html
(cursor keys to move)

It demonstrates the relative movement to the camera.
Nice that you have build in commands for translating coordinates.
Else it would have been hard to calculate it myself.

As you can see something very weird is happening.
The box shakes, as if the ground isn’t flat.
Why does the box not simply move in its X and Z coordinates?


Btw weird that this is allowed:

Instantiate (respawnPrefab, respawn.position, respawn.rotation);

I’d expect that .position and .rotation are part of Transform (which is part of GameObject):

Instantiate (respawnPrefab, respawn.transform.position, respawn.transform.rotation);

If you want to have the box with rigidbody, you really shouldnt modify the position directly. To the physics simulation this seems like you are warping the object around not moving it.

Either use the character controller or use a rigidbody with add force.

You could also try using rigidbody.freezeRotation = true; to make the rigidbody never modify the rotation of the transform.

freezeRotation: Brilliant option.
Thanks for the info! I will go for physics then!