I keep getting null reference

Here is my error:

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

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace RhinoRunnerGaming
{
    public class weapons : MonoBehaviour
    {
        #region Variables

        public gun[] loadout;
        public Transform weaponParent;
        public GameObject bulletHolePrefab;
        public LayerMask canBeShot;

        private int currentIndex;
        private GameObject currentWeapon;

        #endregion

        #region Monobehavior Callbacks

        void Update()
        {
            if (Input.GetKeyDown(KeyCode.Alpha1))
            {
                Equip(0);
            }

            if (currentWeapon != null)
            {
                Aim(Input.GetMouseButton(1));

                if (Input.GetMouseButtonDown(0))
                {
                    Shoot();
                }
            }

        }

        #endregion

        #region Private Methods

        void Equip(int p_ind)
        {
            if (currentWeapon != null) Destroy(currentWeapon);

            currentIndex = p_ind;

            GameObject t_newWeapon = Instantiate(loadout[p_ind].prefab, weaponParent.position, weaponParent.rotation, weaponParent) as GameObject;
            t_newWeapon.transform.localPosition = Vector3.zero;
            t_newWeapon.transform.localEulerAngles = Vector3.zero;

            currentWeapon = t_newWeapon;
        }
        void Aim(bool p_isAiming)
        {
            Transform t_anchor = currentWeapon.transform.Find("anchor");
            Transform t_state_ads = currentWeapon.transform.Find("states/ADS");
            Transform t_state_hip = currentWeapon.transform.Find("states/hips");

            if (p_isAiming)
            {
                //aim
                t_anchor.position = Vector3.Lerp(t_anchor.position, t_state_ads.position, Time.deltaTime * loadout[currentIndex].aimspeed);
            }
            else
            {
                //hip
                t_anchor.position = Vector3.Lerp(t_anchor.position, t_state_hip.position, Time.deltaTime * loadout[currentIndex].aimspeed);
            }
        }

        void Shoot()
        {
            Transform t_spawn = transform.Find("Cameras/playerCamera");

            RaycastHit t_hit = new RaycastHit();
            if (Physics.Raycast(t_spawn.position, t_spawn.forward, out t_hit, 1000f, canBeShot))
            {
                GameObject t_newHole = Instantiate(bulletHolePrefab, t_hit.point + t_hit.normal * 0.001f, Quaternion.identity) as GameObject;
                t_newHole.transform.LookAt(t_hit.point + t_hit.normal);
                Destroy(t_newHole, 5f);
            }
        }

        #endregion
    }

}

can somebody please help me:)

On what line the error appears ?

lines 82 and 37

lines 82 and 37

Looks like

Transform t_spawn = transform.Find("Cameras/playerCamera");

Is not finding any object with the name “Cameras/playerCamera”

Try “playerCamera” since i guess this is the name of a GameObject

Or better don’t find objects by name.

And the most important, learn how to use debugger.

Can you try changing line 81 to the following, removing the “new” declaration:

RaycastHit t_hit;

May not be the issue but worth a try. I’ve never declared a raycasthit as new before and never had any issues with getting them to work.

Only other thing I could think it might be - are you sure the reference to t_spawn is getting set correctly beforehand?

thank you so much, that was the problem!

my camera is not Cameras it’s just camera/playerCamera
simple mistake