NullReferenceException: Object reference not set to an instance of an object GunSystem.Update () (at Assets/GunSystem.cs:39) ,i cant figure out what to reference

when i try to refrerence an object i cant figure out what to refrerence in the script , and how to refrerence a object/ part of script

and heres the script if needed to find the solution (and yes i folowed a tutorial):

using TMPro;
using UnityEngine;

public class GunSystem : MonoBehaviour
{
    //Gun stats
    public int damage;
    public float timeBetweenShooting, spread, range, reloadTime, timeBetweenShots;
    public int magazineSize, bulletsPerTap;
    public bool allowButtonHold;
    int bulletsLeft, bulletsShot;

    //bools 
    bool shooting, readyToShoot, reloading;

    //Reference
    public Camera fpsCam;
    public Transform attackPoint;
    public RaycastHit rayHit;
    public LayerMask whatIsEnemy;

    //Graphics
    public GameObject muzzleFlash, bulletHoleGraphic;
    public CamShake camShake;
    public float camShakeMagnitude, camShakeDuration;
    public TextMeshProUGUI text;

    private void Awake()
    {
        bulletsLeft = magazineSize;
        readyToShoot = true;
    }
    private void Update()
    { 
        MyInput();

        //SetText
        text.SetText(bulletsLeft + " / " + magazineSize);
    }
    private void MyInput()
    {
        if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
        else shooting = Input.GetKeyDown(KeyCode.Mouse0);

        if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();

        //Shoot
        if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
        {
            bulletsShot = bulletsPerTap;
            Shoot();
        }
    }
    private void Shoot()
    {
        readyToShoot = false;

        //Spread
        float x = Random.Range(-spread, spread);
        float y = Random.Range(-spread, spread);

        //Calculate Direction with Spread
        Vector3 direction = fpsCam.transform.forward + new Vector3(x, y, 0);

        //RayCast
        if (Physics.Raycast(fpsCam.transform.position, direction, out rayHit, range, whatIsEnemy))
        {
            Debug.Log(rayHit.collider.name);

            if (rayHit.collider.CompareTag("Enemy"))
                rayHit.collider.GetComponent<ShootingAi>().TakeDamage(damage);
        }

        //ShakeCamera
        camShake.Shake(camShakeDuration, camShakeMagnitude);

        //Graphics
        Instantiate(bulletHoleGraphic, rayHit.point, Quaternion.Euler(0, 180, 0));
        Instantiate(muzzleFlash, attackPoint.position, Quaternion.identity);

        bulletsLeft--;
        bulletsShot--;

        Invoke("ResetShot", timeBetweenShooting);

        if (bulletsShot > 0 && bulletsLeft > 0)
            Invoke("Shoot", timeBetweenShots);
    }
    private void ResetShot()
    {
        readyToShoot = true;
    }
    private void Reload()
    {
        reloading = true;
        Invoke("ReloadFinished", reloadTime);
    }
    private void ReloadFinished()
    {
        bulletsLeft = magazineSize;
        reloading = false;
    }

    private class ShootingAi
    {
        internal void TakeDamage(int damage)
        {
            throw new System.NotImplementedException();
        }
    }
}

public class CamShake
{
    internal void Shake(float camShakeDuration, float camShakeMagnitude)
    {
        throw new System.NotImplementedException();
    }
}

It’s difficult for anyone to be able to help without actually knowing what line of code is 39. Properly formatting the code would help slightly, but adding a comment would be best.
Please don’t require people to do extra work to figure that out.

In the meantime I have a resource for NullReferenceException that you can go through here, which teaches you about stack traces, reference types, common problems, etc.

A NullReferenceException typically occurs when you try to access or use an object that has not been initialized or assigned a value. In your case, the exception is happening in the GunSystem script at line 39. To resolve the issue, you need to identify which object is causing the null reference and ensure that it is properly initialized before accessing its properties or methods.

Here are a few steps you can take to debug and fix the issue:

Check line 39 of the GunSystem.cs script: Look for any variable or object being used on that line. It could be something like gunObject or gunComponent.

Ensure the object is assigned: Make sure that the object in question has been assigned a value or initialized properly before line 39 is executed. Check the code where the GunSystem script is being used to ensure that the necessary assignments are being made.

Debugging with breakpoints: Use a debugger to set breakpoints in your code. Run the program in debug mode and check the state of the variables at line 39. This will help you identify which object is null and causing the exception.

Null check: If you determine that the object is expected to be null in certain cases, add a null check before using it to avoid the exception. You can use an if statement or the null conditional operator (?.) to safely handle null references.

Here’s an example of using a null check:

if (gunObject != null)
{
    // Access properties or methods of gunObject
    gunObject.SomeMethod();
}

By following these steps, you should be able to identify the object causing the null reference and take the necessary steps to resolve the issue.

As the bulletsLeft and magazineSize fields are ints, they will default to 0,
So, probably the field public TextMeshProUGUI text; is null, did you set it in the inspector?

Try replacing this

with this

private void Update()
{ 
    MyInput();
    return; // remove after debugging
    //SetText
    text.SetText(bulletsLeft + " / " + magazineSize);
}

Save and run in unity,
If you are still getting NullReferenceException, the the problem is in MyInput() method,
otherwise it will be the var text, its null (not set).

for all who are wondering how i fixed it i have no idea!!