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.
Answer by Monsoonexe · Apr 03 at 12:48 AM
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: https://www.youtube.com/watch?v=rjNDXKgbhgY
Your answer
![](https://koobas.hobune.stream/wayback/20220613054938im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Loop back to beginning of list in monopoly style game 1 Answer
If Statement within a while loop only checks the if condition once. 0 Answers
Coroutine Not Starting - Crashes Instead? 1 Answer
Switching items - can't detect a crazy loop that is happening. 1 Answer
naming days of the week, how to iterate through strings? 2 Answers