Looping problems

Just gonna post this whole script cuz I have no clue what’s up. I copied a script from a YouTube gun tutorial and it didn’t work properly, it infinitely looped and wouldn’t burst at all. I added in the destroy stuff and commented out it asking for things too, I don’t think that’s affecting it though.

I’ve been messing with it a bunch and it straight up seems like it’s just not working right. I want it to loop as many times as the bulletsPerTap thing is set to. No matter what I do though it either tries to run infinitely and locks up Unity or does a wonky burst that’s more than I wanted it to then won’t let me shoot again.

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

public class GunBase : MonoBehaviour

{
    public int damage;
    public float shootTime, spread, range, reloadTime, shotSpeed;
    public int magSize, shotsPerTap;
    public bool allowHold;
    int bulletsLeft, bulletsFired;

    bool shooting, readyToShoot, reloading;

    public Camera cam;
    public Transform attackpoint;
    public RaycastHit rayHit;
    public LayerMask enemy;

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

    private GameObject[] holeList;
    public int maxHoles;

    private void Awake()
    {
        bulletsLeft = magSize;
        readyToShoot = true;
    }


    private void Update()
    {
        MyInput();
        text.SetText(bulletsLeft + "/" + magSize);
    }

    private void MyInput()
    {
        if (allowHold) shooting = Input.GetKey(KeyCode.Mouse0);
        else shooting = Input.GetKeyDown(KeyCode.Mouse0);

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

        if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
        {
            for (bulletsFired = shotsPerTap; bulletsFired > 0; bulletsFired--)
            {
               Invoke("Shoot", shotSpeed);
            }       
        }
    }

    private void Shoot()
    {
        readyToShoot = false;

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

        Vector3 direction = cam.transform.forward + new Vector3(x, y, 0);

        if (Physics.Raycast(cam.transform.position, cam.transform.forward, out rayHit, range, enemy))
            rayHit.collider.CompareTag("enemy");
               // rayHit.collider.GetComponent<ShootingAI>().TakeDamage(damage);
        

       // camShake.Shake(camShakeDuration, camShakeMagnitude);

        Instantiate(bulletHole, rayHit.point, Quaternion.Euler(0, 180, 0));
        

        Instantiate(muzzleFlash, attackpoint.position, Quaternion.identity);
                if (GameObject.Find ("flash(Clone)") != null) 
        {
            Destroy(GameObject.Find ("flash(Clone)"), .1f);
        }

        bulletsLeft--;

        
            Invoke("ResetShot", shootTime);
        
         

        holeList = GameObject.FindGameObjectsWithTag("Hole");
        ClearHoles();


    }

    private void ClearHoles()
    {
        if (holeList[maxHoles])
        {
            Destroy(GameObject.Find("hole(Clone)"));
        }
    }


    private void ResetShot()
    {
        readyToShoot = true;
    }

    private void Reload()
    {
        reloading = true;
        Invoke("ReloadFinished", reloadTime);
    }

    private void ReloadFinished()
    {
        bulletsLeft = magSize;
        reloading = false;
    }



}

Lol, just tried moving bulletsFired-- from the for to Shoot() and my entire computer immediately crashed when I tried it. Finally got that Windows update and Firefox saved this though.

Just by reading the code, I can’t see an infinite loop. Any for-loops or while-loops can cause them, so just check your logic and make sure it makes sense. Make sure “shotsPerTap” isn’t something huge like 1000 or it could run for a long time.

To debug infinite loops, start commenting out lines of code until the problem disappears. //. Or, if you want to be a boss, look into learning how to attach Visual Studio’s debugger so you can step through the code line by line as it is executing: Visual Studio & Unity: Debugging Your Game - YouTube