Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Error when making a player movement script??

Discussion in 'Scripting' started by CoopReed, Dec 10, 2015.

  1. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    When I was making this script it came up with and error and I have no Idea what is wrong?? Anyhelp would be great.

    Error: NullReferenceException: Object reference not set to an instance of an object

    Code :
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     public float speed = 6f;            // The speed that the player will move at.
    6.  
    7.     Vector3 movement;                   // The vector to store the direction of the player's movement.
    8.     Animator anim;                      // Reference to the animator component.
    9.     Rigidbody playerRigidbody;          // Reference to the player's rigidbody.
    10.     int floorMask;                      // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
    11.     float camRayLength = 100f;          // The length of the ray from the camera into the scene.
    12.  
    13.     void Awake()
    14.     {
    15.         // Create a layer mask for the floor layer.
    16.         floorMask = LayerMask.GetMask("Floor");
    17.  
    18.         // Set up references.
    19.         anim = GetComponent<Animator>();
    20.         playerRigidbody = GetComponent<Rigidbody>();
    21.     }
    22.  
    23.  
    24.     void FixedUpdate()
    25.     {
    26.         // Store the input axes.
    27.         float h = Input.GetAxisRaw("Horizontal");
    28.         float v = Input.GetAxisRaw("Vertical");
    29.  
    30.         // Move the player around the scene.
    31.         Move(h, v);
    32.  
    33.         // Turn the player to face the mouse cursor.
    34.         Turning();
    35.  
    36.         // Animate the player.
    37.         Animating(h, v);
    38.     }
    39.  
    40.     void Move(float h, float v)
    41.     {
    42.         // Set the movement vector based on the axis input.
    43.         movement.Set(h, 0f, v);
    44.  
    45.         // Normalise the movement vector and make it proportional to the speed per second.
    46.         movement = movement.normalized * speed * Time.deltaTime;
    47.  
    48.         // Move the player to it's current position plus the movement.
    49.         playerRigidbody.MovePosition(transform.position + movement);
    50.     }
    51.  
    52.     void Turning()
    53.     {
    54.         // Create a ray from the mouse cursor on screen in the direction of the camera.
    55.         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    56.  
    57.         // Create a RaycastHit variable to store information about what was hit by the ray.
    58.         RaycastHit floorHit;
    59.  
    60.         // Perform the raycast and if it hits something on the floor layer...
    61.         if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask))
    62.         {
    63.             // Create a vector from the player to the point on the floor the raycast from the mouse hit.
    64.             Vector3 playerToMouse = floorHit.point - transform.position;
    65.  
    66.             // Ensure the vector is entirely along the floor plane.
    67.             playerToMouse.y = 0f;
    68.  
    69.             // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
    70.             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
    71.  
    72.             // Set the player's rotation to this new rotation.
    73.             playerRigidbody.MoveRotation(newRotation);
    74.         }
    75.     }
    76.  
    77.     void Animating(float h, float v)
    78.     {
    79.         // Create a boolean that is true if either of the input axes is non-zero.
    80.         bool walking = h != 0f || v != 0f;
    81.  
    82.         // Tell the animator whether or not the player is walking.
    83.         anim.SetBool("IsWalking", walking);
    84.     }
    85. }
    86.  
    When I double click the error this line of code comes up:
    Code (CSharp):
    1.    Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    Thankyou
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    NullReferenceException means that you have tried to 'dig in' to an object that does not exist, so when you pull up a line there, you're looking for dots, and checking the item before the dot to see if it could be null, and if so, what conditions would cause it to be null. In this case you have three candidates:
    Camera.main - Camera is a class, it can't be null
    main.ScreenPointToRay - main might be null
    Input.mousePosition - Input is also a class, and can't be null

    So what would cause Camera.main to be null? Check out the documentation on that, and you'll find that Camera.main finds a Camera object that is tagged with MainCamera and enabled. Check out your camera in the scene, and make sure that its tag is MainCamera.
     
  3. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    ô
    Thanks mate!