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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Survival Shooter: NullReferenceException & Issue with Ray and Camera.main.ScreenPointToRay

Discussion in 'Editor & General Support' started by pmcleod, Dec 4, 2018.

?

What is best for the Holidays (I´m sorry but I´ve no idea what this poll is for)

  1. Turkey?

    1 vote(s)
    33.3%
  2. Ham?

    2 vote(s)
    66.7%
  1. pmcleod

    pmcleod

    Joined:
    Apr 12, 2017
    Posts:
    7
    Hi
    I´m having some issues with The second video (Player Character) in the Survival Shooter tutoriall.

    I´ve tried looking up this error in other Posts but haven't had any luck...

    The error that comes up is:
    NullReferenceException: Object reference not set to an instance of an object
    PlayerMovement.Turning () (at Assets/PlayerMovement.cs:66)
    PlayerMovement.FixedUpdate () (at Assets/PlayerMovement.cs:45)

    Specifically, I´m more interested in know the bug in line 66 as line 45 is just the call to the method.


    It may be worth mentioning that the game appears to work up to the end of the Player Character lesson.

    Many thanks in advanced for any help you can provide.
    Cheers,


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

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Camera.main will be null if you do not have an active object with a Camera component attached which is tagged "MainCamera".

    https://docs.unity3d.com/ScriptReference/Camera-main.html

    Also, unless you are frequently switching cameras, you should just grab a reference to Camera.main once and cache it in a separate variable, instead of finding the camera again every time Turning is called. Behind the scenes Camera.main calls FindGameObjectsWithTag, which is very poor performing and not intended for frequent use.
     
    tom-zheng and LlGHT_ like this.
  3. pmcleod

    pmcleod

    Joined:
    Apr 12, 2017
    Posts:
    7
    Hi Joe-Censored
    Thanks a ton for your prompt reply.
    Thing is... I do have the Main Camera Object tagged as "Main Camera"
    upload_2018-12-5_14-17-52.png

    Any other suggestions?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Is line 66 as in the forum the same line 66 in your code?
     
  5. LlGHT_

    LlGHT_

    Joined:
    Mar 29, 2021
    Posts:
    3
    Thanks alot you just saved my Life =)
     
    Joe-Censored likes this.