Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by LV9999_Majin · Apr 02 at 03:22 PM · looping

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

178 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges