0

Not Duplicate

It not duplicate question as it is not asking what is a NullPointerException, But putting a code that raises this error & but tutorial on unity website where it is taken from doesn't have this issue

I am a starter Unity 3D. I am doing unity roll-a-ball tutorial. Following error occurs when I write the script to move the ball.

Unity 5.0, Windows 7

EXCEPTION

NullReferenceException
    UnityEngine.Rigidbody.AddForce (Vector3 force) (at C:/buildslave/unity/build/artifacts/generated/common/modules/NewDynamics.gen.cs:706)
    PlayerController.FixedUpdate () (at Assets/Scripts/PlayerController.cs:20)

SCRIPT

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    // 
    void Start () {
    }

    // Initialization
    void FixedUpdate () {


        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

        Rigidbody role = new Rigidbody ();
        role.AddForce (movement);
    }
}
XCeptable
  • 1,247
  • 6
  • 25
  • 49
  • It seems the line giving the issue is not included in your post. It should be here: NewDynamics.gen.cs, line 706. – Patrick Hofman Apr 09 '15 at 12:52
  • But where the line should be as I copied all code carefully. One thing is that in the tutorial, the syntax for RigidBody line is 'rigidbody.(movemnet);' i.e. the RigiBody object used without declaration. But that doesn't work for me at least, so I used it like Rigidbody role = new Rigidbody (); role.AddForce (movement); – XCeptable Apr 09 '15 at 13:02
  • not line 706 bec this is mere code I put. Appears line 20 has issue i.e. role.AddForce (movement); so movement is null that is supposed to have position parameters from keyboard. How do recover it as that much code is in tutorial and it is working. – XCeptable Apr 09 '15 at 13:14
  • That's because you are using Unity 5. The quick rigidbody accessor got removed. To initialize your role variable use this: `Rigidbody role = GetComponent();` – grexter89 Apr 09 '15 at 20:39
  • 1
    Also in my opinion this question is not a duplicate. Seems like every question with **NullReferenceException** in the title gets marked as duplicate in the last days. – grexter89 Apr 09 '15 at 20:42

0 Answers0