Search Unity

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

Discussion in 'Editor & General Support' started by PeePeePooPooFLOORGANG, Jan 24, 2021.

  1. PeePeePooPooFLOORGANG

    PeePeePooPooFLOORGANG

    Joined:
    Jan 24, 2021
    Posts:
    3
    So im watching a tutorial (since im new to unity) and i copy exactly as he does on the video but when i run the game (this doesnt affect the game) in the console it says this: NullReferenceException: Object reference not set to an instance of an object. these are all of the scripts:
    this is the 'look' script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace Com.HippogriffHedwig.MyGame
    6. {
    7.     public class Look : MonoBehaviour
    8.     {
    9.         public static bool cursorLocked = true;
    10.        
    11.         public Transform player;
    12.         public Transform cams;
    13.         public Transform weapon;
    14.  
    15.         public float xSensitivity;
    16.         public float ySensitivity;
    17.         public float maxAngle;
    18.  
    19.         private Quaternion camCenter;
    20.  
    21.         void Start()
    22.         {
    23.             camCenter = cams.localRotation;
    24.         }
    25.  
    26.         void Update()
    27.         {
    28.             SetY();
    29.             SetX();
    30.             UpdateCursorLock();
    31.         }
    32.  
    33.         void SetY ()
    34.         {
    35.             float t_input = Input.GetAxis("Mouse Y") * ySensitivity * Time.deltaTime;
    36.             Quaternion t_adj = Quaternion.AngleAxis(t_input, -Vector3.right);
    37.             Quaternion t_delta = cams.localRotation * t_adj;
    38.            
    39.             if (Quaternion.Angle(camCenter, t_delta) < maxAngle)
    40.             {
    41.                 cams.localRotation = t_delta;
    42.             }
    43.  
    44.             weapon.rotation = cams.rotation;
    45.         }
    46.         void SetX()
    47.         {
    48.             float t_input = Input.GetAxis("Mouse X") * xSensitivity * Time.deltaTime;
    49.             Quaternion t_adj = Quaternion.AngleAxis(t_input, Vector3.up);
    50.             Quaternion t_delta = player.localRotation * t_adj;
    51.             player.localRotation = t_delta;
    52.         }
    53.  
    54.         void UpdateCursorLock ()
    55.         {
    56.             if(cursorLocked)
    57.             {
    58.                 Cursor.lockState = CursorLockMode.Locked;
    59.                 Cursor.visible = false;
    60.  
    61.                 if (Input.GetKeyDown(KeyCode.Escape))
    62.                 {
    63.                     cursorLocked = false;
    64.                 }
    65.             }
    66.             else
    67.             {
    68.                 Cursor.lockState = CursorLockMode.None;
    69.                 Cursor.visible = true;
    70.  
    71.                 if (Input.GetKeyDown(KeyCode.Escape))
    72.                 {
    73.                     cursorLocked = true;
    74.                 }
    75.             }
    76.         }
    77.     }
    78. }
    this is the 'motion' script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace Com.HippogriffHedwig.MyGame
    6. {
    7.     public class Motion : MonoBehaviour
    8.     {
    9.         public float speed;
    10.         public float sprintModifier;
    11.         public float jumpForce;
    12.         public Camera normalCam;
    13.         public Transform groundDetector;
    14.         public LayerMask ground;
    15.  
    16.         public Rigidbody rig;
    17.        
    18.         private float baseFOV;
    19.         private float sprintFOVModifier = 1.5f;
    20.  
    21.         private void Start()
    22.         {
    23.             baseFOV = normalCam.fieldOfView;
    24.             Camera.main.enabled = false;
    25.             rig = GetComponent<Rigidbody>();
    26.         }
    27.         void FixedUpdate()
    28.         {
    29.             //Axis
    30.             float t_hmove = Input.GetAxisRaw("Horizontal");
    31.             float t_vmove = Input.GetAxisRaw("Vertical");
    32.  
    33.            
    34.             //Controls
    35.             bool sprint = Input.GetKey(KeyCode.LeftShift);
    36.             bool jump = Input.GetKeyDown(KeyCode.Space);
    37.  
    38.  
    39.             //States
    40.             bool isGrounded = Physics.Raycast(groundDetector.position, Vector3.down, 0.1f, ground);
    41.             bool isJumping = jump && isGrounded;
    42.             bool isSprinting = sprint && t_vmove > 0 && !isJumping && isGrounded;
    43.  
    44.  
    45.             //Jumping
    46.             if (isJumping)
    47.             {
    48.                 rig.AddForce(Vector3.up * jumpForce);
    49.             }
    50.  
    51.            
    52.             //Movement
    53.             Vector3 t_direction = new Vector3(t_hmove, 0, t_vmove);
    54.             t_direction.Normalize();
    55.  
    56.             float t_adjustedSpeed = speed;
    57.             if (isSprinting) t_adjustedSpeed *= sprintModifier;
    58.            
    59.             Vector3 t_targetVelocity = transform.TransformDirection(t_direction) * t_adjustedSpeed * Time.deltaTime;
    60.             t_targetVelocity.y = rig.velocity.y;
    61.             rig.velocity = t_targetVelocity;
    62.  
    63.  
    64.             //FOV
    65.             if (isSprinting) { normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV * sprintFOVModifier, Time.deltaTime * 8f); }
    66.             else { normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV, Time.deltaTime * 8f); }
    67.         }
    68.     }
    69. }  
    this is the 'weapon' script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. namespace Com.HippogriffHedwig.MyGame
    7. {
    8.     public class Weapon : MonoBehaviour
    9.     {
    10.         public Gun[] loadout;
    11.         public Transform weaponParent;
    12.  
    13.         private GameObject currentWeapon;
    14.  
    15.         void Start()
    16.         {
    17.  
    18.         }
    19.  
    20.         void Update()
    21.         {
    22.             if (Input.GetKeyDown(KeyCode.Alpha1)) Equip(0);
    23.         }
    24.         void Equip(int p_ind)
    25.         {
    26.             if (currentWeapon != null) Destroy(currentWeapon);
    27.  
    28.             GameObject t_newWeapon = Instantiate(loadout[p_ind].prefab, weaponParent.position, weaponParent.rotation, weaponParent) as GameObject;
    29.             t_newWeapon.transform.localPosition = Vector3.zero;
    30.             t_newWeapon.transform.localEulerAngles = Vector3.zero;
    31.  
    32.             currentWeapon = t_newWeapon;
    33.         }
    34.     }
    35. }
    36.  
    and finnaly, this is the 'gun' code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace Com.HippogriffHedwig.MyGame
    6. {
    7.     [CreateAssetMenu(fileName = "New Gun", menuName = "Gun")]
    8.     public class Gun : ScriptableObject
    9.     {
    10.         public new string name;
    11.         public float firerate;
    12.         public GameObject prefab;
    13.     }
    14. }
    any help is greatly apriciated!!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    That's a lot of code! Most likely with a NullRef exception you simply forgot to drag and drop an object into an inspector field. Look at your error message, it will have a line number that points to some of your code. Go look at the code on that file and line number. It should give you a pretty good idea what variable is null. That is the variable you probably forgot to assign in the inspector.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    This is it 99.9% of the time for sure. But the good news is, the answer for nullref is always the same... ALWAYS. It is the single most common error ever. Don't waste your life on this problem. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
  4. PeePeePooPooFLOORGANG

    PeePeePooPooFLOORGANG

    Joined:
    Jan 24, 2021
    Posts:
    3
    Thank you so much!
     
  5. PeePeePooPooFLOORGANG

    PeePeePooPooFLOORGANG

    Joined:
    Jan 24, 2021
    Posts:
    3
    Thank you too both comments have been helpull!