Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NullReferenceException: Object reference not set to an instance of an object PlayerMovementScript.Up

Discussion in 'Scripting' started by Stihaabr, Dec 22, 2014.

  1. Stihaabr

    Stihaabr

    Joined:
    Aug 30, 2014
    Posts:
    4
    I get this error message : NullReferenceException: Object reference not set to an instance of an object PlayerMovementScript.Update () (at Assets/PlayerMovementScript.js:6)

    And the code is:
    MouseLookScript.js:

    #pragma strict

    var lookSensitivity : float = 5;
    @HideInInspector
    var yRotation : float;
    @HideInInspector
    var xRotation : float;
    @HideInInspector
    var currentYRotation : float;
    @HideInInspector
    var currentXRotation : float;
    @HideInInspector
    var yRotationV : float;
    @HideInInspector
    var xRotationV : float;
    var lookSmoothDamp : float = 0.1;

    function Update() {

    yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
    xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;

    xRotation = Mathf.Clamp(xRotation, -90, 90);

    currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
    currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);

    transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
    }

    PlayerMovementScript.js:
    var walkAccelaration : float = 5;
    var cameraObject : GameObject;

    function Update ()
    {
    transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
    rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAccelaration, 0, Input.GetAxis("Vertical") * walkAccelaration);
    }
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It's probably the cameraObject in your PlayerMovementScript. Drag the camera onto it.
     
    Last edited: Dec 22, 2014
  3. Stihaabr

    Stihaabr

    Joined:
    Aug 30, 2014
    Posts:
    4
    I have tried that didn't work
     
  4. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Object not set to an instance of an object = what you tried to call doesn't exist. Make sure the component you're calling is on the object, you have the correct object, and there aren't any typos. Then restart unity
     
  5. KyleOlsen

    KyleOlsen

    Joined:
    Apr 3, 2012
    Posts:
    237
    Does player have a rigidbody on it?
    Does cameraObject have a MouseLookScript on it?