Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jkm8 · May 21, 2016 at 06:07 PM · error messageshootingweapon systemworkdoesnt

DivideByZero Expection: Division by zero?

 using UnityEngine;
 using System.Collections;
 
 public class WeaponShoot : MonoBehaviour {
     public PlayerWeapon[] Weapons;
 
 
     AudioSource audiosource;
 
     private RaycastHit hit;
     private bool Firing = false;
     private bool Reloading = false;
     private PlayerWeapon curWeapon;
 
     public float recoil = 50f;
     public AudioClip shoot;
 
 
 
     void Awake()
     {
         ChangeWeaponToSlot (0);
         audiosource = GetComponent<AudioSource> ();
     }
     void Update()
     {
 
         if(Input.GetKeyDown(KeyCode.R) && !Reloading)
         {
             StartCoroutine(isReloading());
         }
         else if (Input.GetButton("Fire1") && !Firing)
         {
             StartCoroutine(isFiring());
         }
 
 
     }
 
     void shot () {
 
         Vector3 myTransform = transform.transform.forward;
         Physics.Raycast (transform.position, myTransform, out hit, 50);
         if ((Physics.Raycast (transform.position, myTransform, out hit, 50)) && (hit.collider.gameObject.tag == ("Enemy")))
         {
             hit.collider.SendMessageUpwards("DoDamage");
             Debug.Log ("hit");
         }
     }
     private IEnumerator isFiring()
     {
         
         float rateOfFirePerSecond = 1 / (curWeapon.fireRate / 60);
         Firing = true;
         Camera.main.transform.Rotate( recoil * Time.deltaTime, 0, 0 );
 
         while (Input.GetMouseButton(0) && curWeapon.CurAmmo > 0)
         {
             Fire();
             yield return new WaitForSeconds(rateOfFirePerSecond);
         }
         Firing = false;
     }
 
 
     private IEnumerator isReloading ()
     {
         float reloadTimePerBullet = curWeapon.ReloadTime / curWeapon.AmmoMax;
         Reloading = true;
 
         while (curWeapon.AmmoMax > 0 && curWeapon.CurAmmo < curWeapon.ClipSize)
         {
             Reload();
             yield return new WaitForSeconds(reloadTimePerBullet);
         }
         Reloading = false;
     }
 
 
     void Fire()
     {
         Vector3 forwardLocal = transform.forward;
 
 
         if ((Physics.Raycast(transform.position, forwardLocal, out hit, 50)) && (hit.collider.gameObject.tag == ("enemy")))
         {
             hit.collider.GetComponent<AiHealthScript>().DoDamage(curWeapon.Damage);
         }
 
 
         audiosource.PlayOneShot(curWeapon.GunShot, 0.7f);
         curWeapon.CurAmmo--;
     }
 
 
     void Reload()
     {
         curWeapon.AmmoMax--;
         curWeapon.CurAmmo++;
     }
 
 
     void ChangeWeaponToSlot(int index)
     {
         if(Weapons.Length > index)
         {
             curWeapon = Weapons[index];
         }
     }
 
 
 }

whenever i run my game and try to hit an enemy it gives me this error: DivideByZero Expection: Division by zero

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 jgodfrey · May 21, 2016 at 06:28 PM

What line in the above code is causing the error? Assuming the error is in the above code, it's probably one of the following lines:

 float rateOfFirePerSecond = 1 / (curWeapon.fireRate / 60);

or...

  float reloadTimePerBullet = curWeapon.ReloadTime / curWeapon.AmmoMax;

What types are the above "curWeapon" members? If they are of type "int", you're likely being bit by the fact that the math will be done using only ints. If that's the problem, you need to ensure that at least one value in the expression is of type float. Then, the compiler will generate a float output.

If the crash is in the first line above, changing the "60" to "60.0f" should fix the problem. If the problem is in the second line above, you need to promote at least one of the values to a float. For instance, you could change it to:

 float reloadTimePerBullet = curWeapon.ReloadTime / (float)curWeapon.AmmoMax;
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

I need help with untiy in the newest firefox version. 0 Answers

Keep getting an error saying game object is missing but everything is working just fine just a spam of errors. 0 Answers

What is wrong with this why can't i move? 0 Answers

Ball doesn't roll in roll-a-ball for mac 0 Answers

Shooting Gun Script 4 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