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
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;
Your answer
Follow this Question
Related Questions
I need help with untiy in the newest firefox version. 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