Need help with fixing an issue that happens in build but not editor

When I do a build, it make the enemies in my game take extremely long to kill when they don’t in the editor, I don’t know why as I have made sure everything I could find works in the editor when the build just kind of ruins it. I will post the damage code and the enemy code to just show it incase there is an issue there. Thank you if you can help.

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

public class Entity : MonoBehaviour
{
    [SerializeField] private float StartingHealth;
    private float health;

    public float Health
    {
        get
        {
            return health;
        }
        set
        {
            health = value;
            Debug.Log(health);

            if(health <= 0f)
            {
                Destroy(gameObject);
            }
        }
    }





    void Start()
    {
        Health = StartingHealth;
    }

    
    void Update()
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DamageGun : MonoBehaviour
{
    public float Damage;
    public float bulletRange;
    private Transform PlayerCamera;

    public Camera cam;
    public bool useShotgun = false; 
    public int pellets = 10; 
    public float spreadAngle = 20f; 

    void Start()
    {
        PlayerCamera = Camera.main.transform;
    }

    public void Shoot()
    {
        if (useShotgun)
        {
            ShootShotgun();
        }
        else
        {
            ShootSingleBullet();
        }
    }

    private void ShootSingleBullet()
    {
        Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
        RaycastHit hit;

        Vector3 targetPoint;
        if (Physics.Raycast(ray, out hit))
            targetPoint = hit.point;
        else
            targetPoint = ray.GetPoint(75);

        Ray gunRay = new Ray(PlayerCamera.position, PlayerCamera.forward);
        if (Physics.Raycast(gunRay, out RaycastHit hitInfo, bulletRange))
        {
            if (hitInfo.collider.gameObject.TryGetComponent(out Entity enemy))
            {
                enemy.Health -= Damage;
            }
        }
    }

    private void ShootShotgun()
    {
        for (int i = 0; i < pellets; i++)
        {
            
            float spreadX = Random.Range(-spreadAngle / 2, spreadAngle / 2);
            float spreadY = Random.Range(-spreadAngle / 2, spreadAngle / 2);

            
            Vector3 direction = Quaternion.Euler(spreadY, spreadX, 0) * PlayerCamera.forward;

            
            if (Physics.Raycast(PlayerCamera.position, direction, out RaycastHit hit, bulletRange))
            {
                if (hit.collider.gameObject.TryGetComponent(out Entity enemy))
                {
                    enemy.Health -= Damage;
                }
            }
        }
    }
    private void OnDrawGizmosSelected()
    {

        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, bulletRange);

    }
}

I’m guessing that you’re calling the ‘Shoot’ method when a button is held down instead of only when a button is initially pressed/clicked. So maybe GetButton instead of GetButtonDown?. And because your frame rate is much higher in the editor than in a build it means the Shoot method is being called more often.

You may be able to fix the frame rate difference by enabling VSync in the game view. But the real issue is probably down to using GetButton instead of GetButtonDown. Or if you want to be able to keep shooting while the button is held down then you’ll need to add a fire rate timer.

Well, I need the shoot to allow automatic firing, how would I add in a fire rate timer?

float shootTime;

public void Shoot()
{
	if (Time.time<shootTime)
		return;
	if (useShotgun)
	{
		ShootShotgun();
		shootTime=Time.time+1f;
	}
	else
	{
		ShootSingleBullet();
		shootTime=Time.time+0.5f; // faster rate for single shots
	}
}

Would that work well for automatic in this script? The “DamageGun” script is pretty much an extension of this script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine.Events;
using UnityEngine;

public class Gun : MonoBehaviour
{
    public UnityEvent OnGunShoot;
    public float fireCoolDown;

    public bool Automatic;

    private float currentCooldown;





    void Start()
    {
        currentCooldown = fireCoolDown;
    }


    void Update()
    {
        if(Automatic)
        {
            if(Input.GetMouseButton(0))
            {
                if(currentCooldown <= 0f)
                {
                    OnGunShoot?.Invoke();
                    currentCooldown = fireCoolDown;
                }
            }
        }
        else if (Input.GetMouseButtonDown(0))
        {
            if (currentCooldown <= 0f)
            {
                OnGunShoot?.Invoke();
                currentCooldown = fireCoolDown;
            }
        }

        if (Automatic)
        {
            if (Input.GetKey(KeyCode.JoystickButton5))
            {
                if (currentCooldown <= 0f)
                {
                    OnGunShoot?.Invoke();
                    currentCooldown = fireCoolDown;
                }
            }
        }
        else if (Input.GetKeyDown(KeyCode.JoystickButton5))
        {
            if (currentCooldown <= 0f)
            {
                OnGunShoot?.Invoke();
                currentCooldown = fireCoolDown;
            }
        }

        currentCooldown -= Time.deltaTime;
    }



}

You’ve already got a cool down implemented and so you shouldn’t need my code. Use the inspector to set the fireCoolDown value. If that doesn’t work then it’s time to use Debug.Log to find out why…

Well, the gun fires per 0.1 seconds, I have tried other guns with one second cooldown that should one shot, it is not an issue with how often the shoot is used. I had the cooldown on the whole time, I just forgot.

I did some debugging, it seems like the raycast does not even hit the enemy for most of the shots, this shouldn’t happen as it always hits in the editor, any idea to work around this bug?

Don’t stop, you’re not done! Keep debugging! For instance:

What does it hit? You? Another collider? Nothing? Something else? Place a GIANT wall behind the enemy. Can you hit it? Why not? How long is the ray? Is it pointed in the right direction? etc. etc. etc.

Time to REALLY start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Well, it seems the shots don’t even fire half the time.

Find out why. You can see from just a TINY slice of possible questions to answer above that there is no way one can do:

When you don’t even know the bug!! Input? Input connections? Timing? Framerate? Code exception being thrown? Something else?

Yeah, well, I don’t even know what to look for, inputs seem to work, turns out the bullets are firing, enemies seem to only be getting hit once every 20 shots. Imma try the wall behind them.

I have debugged it to the point that I found out it is not being detected when it hits the enemy 90% of the time, the shot is being fired, I am thinking I should make a new post with this new information.