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

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

Discussion in 'Scripting' started by dragonice501, Dec 2, 2015.

  1. dragonice501

    dragonice501

    Joined:
    Dec 2, 2015
    Posts:
    146
    So i get a null reference exception whenever i run the game but can't figure out what's throwing it.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TP_Camera : MonoBehaviour
    5. {
    6.     public static TP_Camera Instance;
    7.  
    8.     public Transform TargetLookAt;
    9.     public float Distance = 5;
    10.     public float DistanceMin = 3;
    11.     public float DistanceMax = 10;
    12.     public float DistanceSmooth = 0.05f;
    13.     public float X_MouseSensitivity = 5;
    14.     public float Y_MouseSensitivity = 5;
    15.     public float MouseWheelSensitivity = 5;
    16.     public float X_Smooth = 0.05f;
    17.     public float Y_Smooth = 0.1f;
    18.     public float Y_MinLimit = -40;
    19.     public float Y_MaxLimit = 80;
    20.  
    21.  
    22.     private float mouseX = 0;
    23.     private float mouseY = 0;
    24.     private float velX = 0;
    25.     private float velY = 0;
    26.     private float velZ = 0;
    27.     private float velDistance = 0;
    28.     private float startDistance = 0;
    29.     private float desiredDistance = 0;
    30.     private Vector3 desiredPosition = Vector3.zero;
    31.     private Vector3 position = Vector3.zero;
    32.  
    33.  
    34.     void Awake()
    35.     {
    36.         Instance = this;
    37.     }
    38.  
    39.     // Use this for initialization
    40.     void Start ()
    41.     {
    42.         Distance = Mathf.Clamp (Distance, DistanceMin, DistanceMax);
    43.         startDistance = Distance;
    44.         Reset ();
    45.     }
    46.    
    47.     // Update is called once per frame
    48.     void LateUpdate ()
    49.     {
    50.         if (TargetLookAt = null)
    51.             return;
    52.  
    53.         HandlePlayerInput ();
    54.  
    55.         CalculateDesiredPosition ();
    56.  
    57.         UpdatePosition ();
    58.     }
    59.  
    60.     void HandlePlayerInput()
    61.     {
    62.         var deadZone = 0.01;
    63.  
    64.         if (Input.GetMouseButton (1))
    65.             mouseX += Input.GetAxis ("Mouse X") * X_MouseSensitivity;
    66.             mouseY -= Input.GetAxis ("Mouse Y") * Y_MouseSensitivity;
    67.  
    68.         mouseY = Helper.ClampAngle (mouseY, Y_MinLimit, Y_MaxLimit);
    69.  
    70.         if (Input.GetAxis ("Mouse ScrollWheel") < -deadZone || Input.GetAxis ("Mouse ScrollWheel") > deadZone)
    71.             desiredDistance = Mathf.Clamp (Distance - Input.GetAxis ("Mouse Scroll Wheel") * MouseWheelSensitivity, DistanceMin, DistanceMax);
    72.     }
    73.  
    74.     void CalculateDesiredPosition()
    75.     {
    76.         Distance = Mathf.SmoothDamp (Distance, desiredDistance, ref velDistance, DistanceSmooth);
    77.  
    78.         desiredPosition = CalculatePosition (mouseY, mouseX, Distance);
    79.     }
    80.  
    81.     Vector3 CalculatePosition(float rotationX, float rotationY, float distance)
    82.     {
    83.         Vector3 direction = new Vector3 (0, 0, -distance);
    84.         Quaternion rotation = Quaternion.Euler (rotationX, rotationY, 0);
    85.         return TargetLookAt.position + rotation * direction;
    86.     }
    87.  
    88.     void UpdatePosition()
    89.     {
    90.         var posX = Mathf.SmoothDamp (position.x, desiredPosition.x, ref velX, X_Smooth);
    91.         var posY = Mathf.SmoothDamp (position.y, desiredPosition.y, ref velY, Y_Smooth);
    92.         var posZ = Mathf.SmoothDamp (position.z, desiredPosition.z, ref velZ, X_Smooth);
    93.         position = new Vector3 (posX, posY, posZ);
    94.  
    95.         transform.position = position;
    96.  
    97.         transform.LookAt (TargetLookAt);
    98.     }
    99.  
    100.     public void Reset()
    101.     {
    102.         mouseX = 0;
    103.         mouseY = 10;
    104.         Distance = startDistance;
    105.         desiredDistance = Distance;
    106.     }
    107.  
    108.     public static void UseExistingOrCreateNewMainCamera()
    109.     {
    110.         GameObject tempCamera;
    111.         GameObject targetLookAt;
    112.         TP_Camera myCamera;
    113.  
    114.         if (Camera.main != null)
    115.             tempCamera = Camera.main.gameObject;
    116.         else
    117.             tempCamera = new GameObject ("Main Camera");
    118.             tempCamera.AddComponent <Camera>();
    119.             tempCamera.tag = "MainCamera";
    120.  
    121.         tempCamera.AddComponent <TP_Camera>();
    122.         myCamera = tempCamera.GetComponent ("TP_Camera") as TP_Camera;
    123.  
    124.         targetLookAt = GameObject.Find ("targetLookAt") as GameObject;
    125.  
    126.         if (targetLookAt == null)
    127.             targetLookAt = new GameObject ("targetLookAt");
    128.             targetLookAt.transform.position = Vector3.zero;
    129.  
    130.         myCamera.TargetLookAt = targetLookAt.transform;
    131.            
    132.     }
    133. }
    This is the error:

    NullReferenceException: Object reference not set to an instance of an object
    TP_Camera.CalculatePosition (Single rotationX, Single rotationY, Single distance) (at Assets/Scripts/TP_scripts/TP_Camera.cs:85)
    TP_Camera.CalculateDesiredPosition () (at Assets/Scripts/TP_scripts/TP_Camera.cs:78)
    TP_Camera.LateUpdate () (at Assets/Scripts/TP_scripts/TP_Camera.cs:55)

    If you're interested this is the video series I've been copying from. It's meant for a third person game.

    https://www.3dbuzz.com/training/view/3rd-person-character-system
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
  3. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    TargetLookAt isn't assigned at line 85.

    That's because on line 50 you're saying if(TargetLookAt = null) which is setting TargetLookAt to be null

    = : Set a value
    == : Check if values are the same

    Line 50 should be

    if(TargetLookAt == null)
     
  4. dragonice501

    dragonice501

    Joined:
    Dec 2, 2015
    Posts:
    146
    Son of a bitch, it's the little things that screw you.
     
    LeftyRighty likes this.
  5. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    That's how game dev works
     
    LeftyRighty likes this.