NullReferenceException Error, has not been solved yet.

I posted this a while ago, but since the post is old, i’m no longer getting answers. And I haven’t received an answer that has fixed the problem.

What I want my script to do is make the ball move by the means of force and torque, but it doesn’t move anywhere and I get this error whenever I run it.

NullReferenceException Movementscript.FixedUpdate () (at Assets/Movementscript.js:7)

Here is the script for the ball that is supposed to move:

var speed=20.0; 
function FixedUpdate () {

var torque=Vector3(Input.GetAxisRaw("Vertical"), 0, -Input.GetAxisRaw("Horizontal"));
if(torque.magnitude > 0.0){
    torque=Camera.mainCamera.transform.TransformDirection(torque);
    rigidbody.angularVelocity=Vector3.Lerp(rigidbody.angularVelocity, torque * 2, 0.3);
}
if (Input.GetKey("w")) {

rigidbody.AddRelativeTorque (2, 0, 0);

rigidbody.AddRelativeForce (2, 0, 0); }

if (Input.GetKey("s")) {

rigidbody.AddRelativeTorque (-2, 0, 0); rigidbody.AddRelativeForce (-2, 0, 0); }

if (Input.GetKey("a")) { rigidbody.AddRelativeTorque (0, 0, -2); rigidbody.AddRelativeForce (0, 0, -2); }

if (Input.GetKey("d")) { rigidbody.AddRelativeTorque (0, 0, 2); rigidbody.AddRelativeForce (0, 0, 2); } }

And here is the script for the camera that is attached to it:

var target: Transform;
var damp: float = 0.2;
var distance: float = 50;

function Update(){

    damp = Mathf.Clamp(damp, 0.01, 10); // clamps damping factor
    var pCam = transform.position;
    var pTarget = target.position;
    var diff: Vector3 = pTarget - pCam;  // diff = difference between positions
    var dist = diff.magnitude;  // dist = distance between them
    if (Mathf.Abs(diff.y) < 0.7*distance){
        diff.y = 0;  // doesn't modify camera height unless angle > 45
    } 
    if (dist>distance){ // if distance too big...
        diff *= 1-distance/dist; // diff = position error
        // move a FPS independent little step towards the ideal position
        transform.position = pCam + diff * Time.deltaTime/damp;
    }
    transform.LookAt(pTarget);
}

For reference the name of the camera is mainCamera. And the name of the ball is Sphere.
The ball has a rigidbody attached. And lastly the only things in the Scene are the terrain “Terrain”, the ball “Sphere”, the light “Directional Light”, and the camera “mainCamera”.

Help on how to fix this problem so I can get rolling. (Pun intended, laugh here if you want to.) would be a great help. Thanks again UnityAnswers.

@valestrom, I created a new scene, placed the same objects (terrain, sphere+rigidbody, light and mainCamera), attached the scripts to the sphere and the camera, and everything worked fine: the camera run to reach certain distance of the sphere, then followed its movement when I pressed WASD. I saved the camera script initially as Camera.js, but Unity gave a warning that GetComponent would not work because there were a component named Camera already in the project. I renamed it to CameraScript.js and everything worked as expected.

Nothing seems wrong in your script, and in fact it works. My suggestion is: create a new project and scene, place the scripts there and try again. If it still give the same error, try to re-install Unity. There are no rational reasons for this to not work - it worked in my computer (PC Intel Core Duo, Unity 3.3 Indie)

Well why you don’t just look at the line 7 where the error occured?

What is in line 7?.. this:

torque = Camera.mainCamera.transform.TransformDirection(torque);

So just check the line if there’s something wrong. After just 3 sec i can see what’s wrong.
The Camera class doesn’t have a property / variable called mainCamera. It’s called Camera.main. Actually you should get a compiler error… if not then i can add a new point to my list why i never ever would use UnityScript. I don’t get why some ppl say it’s “easier” when you can bump into pitfalls every few steps. In C# with Visual studio it would complain even before you get a chance to test such corrupt code.

Thanks to all of you, UnityAnswers has proven to be a great ************in resource. The whole, putting it in a new scene worked. For whatever reason it did. And it’s flawless, set the torque to 25 and even watch it bounce off the map. You guys are great. Send me an email at blackmethod@gmx.com and you get into my Alpha , if you want.