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
  4. Dismiss Notice

NullReferenceException error

Discussion in 'Scripting' started by Epikmemer45, Oct 18, 2020.

  1. Epikmemer45

    Epikmemer45

    Joined:
    Oct 13, 2020
    Posts:
    9
    I am trying to follow a tutorial trying to get bullet holes to work but it says there is an NullReferenceException error on line 49. Here is my code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class WeaponHandler : MonoBehaviour
    7. {
    8.     public Gun[] loadout;
    9.     public Transform weaponParent;
    10.     public GameObject bulletholePrefab;
    11.     public LayerMask canBeShot;
    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.         if (currentWeapon != null)
    25.         {
    26.             if(Input.GetMouseButtonDown(0))
    27.             {
    28.             Shoot();
    29.             }
    30.         }  
    31.     }
    32.  
    33.     void Equip(int p_id)
    34.     {
    35.         if (currentWeapon != null) Destroy(currentWeapon);
    36.  
    37.         GameObject t_newWeapon = Instantiate(loadout[p_id].prefab, weaponParent.position, weaponParent.rotation, weaponParent) as GameObject;
    38.         t_newWeapon.transform.localPosition = Vector3.zero;
    39.         t_newWeapon.transform.localEulerAngles = Vector3.zero;
    40.  
    41.         currentWeapon = t_newWeapon;
    42.     }
    43.  
    44.     void Shoot()
    45.     {
    46.         Transform t_spawn = transform.Find("Cameras/JimmyCamera");
    47.  
    48.         RaycastHit t_hit = new RaycastHit();
    49.         if(Physics.Raycast(t_spawn.position, t_spawn.forward, out t_hit, 1000f, canBeShot))
    50.         {
    51.             GameObject t_newHole = Instantiate(bulletholePrefab, t_hit.point + t_hit.normal * 0.001f, Quaternion.identity) as GameObject;
    52.             t_newHole.transform.LookAt(t_hit.point + t_hit.normal);
    53.             Destroy(t_newHole, 10f);
    54.         }
    55.     }
    56. }
    57.  
    Can anyone please help me fix this error
     
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    Looks like Line 46 isn't finding
    JimmyCamera
    . You can check this by adding
    print(t_spawn);
    just after Line 46. Post a screencap of your scene's hierarchy. I'm guessing
    JimmyCamera
    isn't where this code thinks it is.

    (Bit of a leap on my part, but try replacing Line 46 with
    t_spawn = GameObject.Find("Cameras/JimmyCamera");
    . Might work.)
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    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.
     
  4. Epikmemer45

    Epikmemer45

    Joined:
    Oct 13, 2020
    Posts:
    9
    I replaced line 46 with your version but it says "t_spawn doesn't exist in current context"
     
  5. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    Oops. Use this instead:

    Transform t_spawn = GameObject.Find("Cameras/JimmyCamera");
     
  6. Epikmemer45

    Epikmemer45

    Joined:
    Oct 13, 2020
    Posts:
    9
    Thank you but now it says that it can't implicitantly convert type UnityEngine.GameObject to UnityEngine.Transform
     
  7. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    Yup. My bad again. Use this:

    Transform t_spawn = GameObject.Find("Cameras/JimmyCamera").transform;
     
  8. Epikmemer45

    Epikmemer45

    Joined:
    Oct 13, 2020
    Posts:
    9
    Awesome thank you
     
    Stevens-R-Miller likes this.