Inconsistent/consistent bug in webGL build (player cant damage enemies FPS)

So I’ve been making a simple FPS game for webGL, and in the latest build a few in my team ( 2 out of 5 ) experience this issue that they cant shoot (damage) enemies. They have similar specs pc and use same browser (tried with Chrome, Brave, FireFox).

The guns are copy pasted, the material is only different. It works (for them) with one gun but dosent with another.
On editor and my browsers (and for the others in the team) works fine.

Normally I would google and modify the script but I’ve been stuck with this bug for two months and it’s especially a hassle to have to build > send to be uploaded on web > and have everyone always test, instead of just seeing if it works on the editor/my pc.
I’m open for better workflows

Any ideas on what to do or any clue what to look for would mean a lot!
P.S. I’m more less a beginner developer with basic coding/unity knowledge, so Step by Step explanations would also be greatly appreciated!
will it get fixed if we change platform perhaps?

more on how it works:
in the main menu the player chooses which gun he will use, and a string (name of the gun) is sent to a script (in a level) that turns on the gun object (gun name = string name) and disables other ones**(gun objects in list)**. My guess ether it’s a ray cast issue (i had one a few months ago) or in worst case scenario a Unity thing.
I dont know if might help but theres also a small issue when the player is pointing a “interactable” object (tag) sometimes the sprite (hand) dosent show up. It works for some object and for others dosent (only the object un that group that is the model is different).

8508122--1133456--upload_2022-10-12_16-15-47.png8508122--1133453--upload_2022-10-12_16-13-59.png

On debug.log the messeage stop at “gun has shot 2”.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class shoot_re : MonoBehaviour {
    [Header("References")]
    [SerializeField] private gun_data gunData;
    [SerializeField] private Transform cam;

    [SerializeField] private KeyCode reloadKey = KeyCode.R;

    //float timeSinceLastShot;
    // public float RateOfFire = 0.25f;
    private float nextShot;
    public ParticleSystem muzzleFlash;
    private Animator gunAnim;

    public LayerMask layerMask;
    private float damageMax;
    private float damageRange;
    private int damageRegular;

    private float damageHead;
    public float damageMaxHead;
    private float damageRangeHead;
    private int damageRegularHead;
   
    //  private AudioSource audioSource;
    public AudioSource gunAudio;
    public AudioClip gunRLoad;

    public Text ammoText;
    public Text magText;
   
    private void Start() {
    //    PlayerShoot.shootInput += Shoot;
    //    PlayerShoot.reloadInput += StartReload;

            gunAudio = GetComponent<AudioSource>();
        //    gunRLoad = gameObject.AddComponent<AudioSource>();
       
        gunAnim = GetComponent<Animator>();

        gunData.currentAmmo = gunData.magSize;
        gunData.magNumb = 2;

        damageMax = gunData.damage + gunData.damage / 2;
    }

    private void OnDisable() => gunData.reloading = false;

    private void StartReload() {
    if (!gunData.reloading && this.gameObject.activeSelf)
        StartCoroutine(Reload());
    }
   
    private IEnumerator Reload() {
    gunData.reloading = true;
    gunAnim.SetTrigger("Reload");
    gunAudio.PlayOneShot(gunRLoad, 0.7f);
    yield return new WaitForSeconds(gunData.reloadTime);
    gunData.currentAmmo = gunData.magSize;
    gunData.magNumb -= 1;
    gunData.reloading = false;
   
    }
   
    // private bool CanShoot() => !gunData.reloading && timeSinceLastShot > 1f / (gunData.fireRate / 60f);
    // private bool Shet = true;
   
    public void Update() {
   
    ammoText.text = gunData.currentAmmo + "";
    magText.text = "" + gunData.magNumb;

    damageRange = Random.Range(gunData.damage, damageMax);
    damageRegular = Mathf.RoundToInt(damageRange);

    if(Input.GetKeyDown(reloadKey) && gunData.magNumb > 0 && gunData.magSize/2 > gunData.currentAmmo) {
        StartReload();
    }
   
    if (Input.GetButtonDown("Fire1") && !gunData.reloading && Time.time > nextShot){
        nextShot = Time.time + gunData.fireRate;

        Debug.Log("gun has shot");
        //RaycastHit hit;       
       
        if (gunData.currentAmmo > 0) {    
        // if(CanShoot()) {
        //Debug.Log("BRUUUUUUUUH");
        gunAnim.SetTrigger("Shoot");
        OnGunShot();
        Debug.Log("gun has shot 2");
       
        if(Physics.Raycast(cam.position, cam.forward, out RaycastHit hit, gunData.maxDistance, layerMask)) {
        //    if (health != null)
        //       {
            ShootableBox health = hit.transform.GetComponent<ShootableBox>();
            bool head = false;

            Debug.Log("ray casted");
           
            if(hit.collider.tag == "Head"){
            //ShootableBox health = hit.transform.GetComponentInParent<ShootableBox>();

            float damageHead = gunData.damage + gunData.damage / 2;
            damageMax = damageHead + gunData.damage;
            damageRange = Random.Range(damageHead, damageMax);
            damageRegular = Mathf.RoundToInt(damageRange);

            damageMax = gunData.damage + gunData.damage / 2;
            health.Damage (damageRegular);

            head = true;
            Debug.Log("shot enemys head");
            }

            if(hit.transform.tag == "Enemy" & !head){
            //ShootableBox health = hit.transform.GetComponent<ShootableBox>();
            damageMax = gunData.damage + gunData.damage / 2;
            health.Damage (damageRegular);
            Debug.Log("shot enemy");
            }

            Debug.Log("shot enemy fin");
            //    }
        }
        gunData.currentAmmo--;
       
        //timeSinceLastShot = 0;
       
        // }
        }else if (gunData.magNumb >0){
        StartReload();
        }
        else{
        Debug.Log("no more ammo");
        }
    }
   
    //private void Shoot() {
    //    timeSinceLastShot += Time.deltaTime;;
    //    }
    }
    private void OnGunShot() {
    //Effects
    gunAudio.time = 0.4f;
    gunAudio.Play();
        muzzleFlash.Play();
    }
}

That’s a great start… but don’t stop, KEEP GOING! Add more Debug.Log() statements and keep adding them until you finish debugging it and find the problem.

You must find a way to get the information you need in order to reason about what the problem is.

Once you find the problem, then you can reason about how to correct the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

When in doubt, print it out!™

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

1 Like

thanks, will give it another go!