Search Unity

Bug Strange problem with WebGL

Discussion in 'Web' started by ivan_uspenskiy, May 14, 2023.

  1. ivan_uspenskiy

    ivan_uspenskiy

    Joined:
    May 14, 2019
    Posts:
    8
    Hi!

    I have a simple project - it's some sort of LCD-games. Everything is based on switching the visibility of elements in order from arrays (with a timer).

    It works perfect on editor - but when i'm doing build for a browser - it's go crazy. Game works, but all logic stunks. I think, all int used for pick elements from arrays is randomizing.. Looks like an absurd.

    It's no compression or stripping issues. It's no version issues (tried on 2021.3.25f1 and 2021.3.19f1). Tried publishing to Unity Play, tried to launch locally with 2 Chrome and Safari.
    I have no idea - what to do..

    Thanks for help in advance
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,899
    You could post the code of interest for one. ;)

    Perhaps you rely on delta time and those may be absurdly high intermittently in WebGL, especially when a scene or asset loads.
     
  3. ivan_uspenskiy

    ivan_uspenskiy

    Joined:
    May 14, 2019
    Posts:
    8
    Thanks for your interest!
    I checked the load time in the editor and in the browser - it is the same (approximately).

    Ok, it's a code fragment from begin and to first bugs..

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6. using TMPro;
    7.  
    8. public class gameManager : MonoBehaviour
    9. {
    10.     private bool isplay;
    11.  
    12.     private int level;
    13.  
    14.     public int score;
    15.     private int hiscore;
    16.     public int lives;
    17.     private float timer;
    18.     private int position;
    19.  
    20.     public TMPro.TextMeshProUGUI scoreText;
    21.     public TMPro.TextMeshProUGUI gameOverText;
    22.     public TMPro.TextMeshProUGUI scoreLabel;
    23.     public TMPro.TextMeshProUGUI hiscoreLabel;
    24.     public TMPro.TextMeshProUGUI youwon;
    25.  
    26.     public GameObject tempRestart;
    27.     public GameObject tempStart;
    28.     public GameObject tempTheEnd;
    29.  
    30.     private bool itstime;
    31.     private bool canspawn;
    32.     private bool canmove;
    33.  
    34.     private int[] vectors = { 0, 1, 2, 3, 4, 5 };
    35.  
    36.     private int ast0 = 0;
    37.     private int ast1 = 0;
    38.     private int ast2 = 0;
    39.     private int ast3 = 0;
    40.     private int ast4 = 0;
    41.     private int ast5 = 0;
    42.  
    43.     private int[] asteroid_states = { 0, 0, 0, 0, 0, 0 };
    44.  
    45.     private int spawnpoint;
    46.  
    47.     private float begin = 2.0f;
    48.     private float easy = 1.5f;
    49.     private float medium = 1f;
    50.     private float hard = 0.75f;
    51.  
    52.  
    53.     private bool scorechange;
    54.  
    55.     public float indicator;
    56.  
    57.     private int endstate;
    58.  
    59.  
    60.  
    61.     // Visualisation
    62.  
    63.     private GameObject[] ships;
    64.     private GameObject[] indicators;
    65.     private GameObject[] lasers;
    66.     private GameObject[] a0;
    67.     private GameObject[] a1;
    68.     private GameObject[] a2;
    69.     private GameObject[] a3;
    70.     private GameObject[] a4;
    71.     private GameObject[] a5;
    72.  
    73.     private GameObject[] e0;
    74.     private GameObject[] e1;
    75.     private GameObject[] e2;
    76.     private GameObject[] e3;
    77.     private GameObject[] e4;
    78.     private GameObject[] e5;
    79.  
    80.     public GameObject life1;
    81.     public GameObject life2;
    82.     public GameObject life3;
    83.  
    84.     private GameObject[] threatlevel;
    85.  
    86.     private GameObject[] badend;
    87.     private GameObject[] goodend;
    88.  
    89.  
    90.     List<GameObject[]> explotions;
    91.  
    92.  
    93.     public GameObject touchbigbutton;
    94.  
    95.     // Sound
    96.  
    97.     public AudioClip hit;
    98.     public AudioClip gameover;
    99.  
    100.     private AudioSource audioSource;
    101.  
    102.     public AudioClip[] shootsounds;
    103.     public AudioClip[] temps;
    104.  
    105.     public AudioClip badendsound;
    106.     public AudioClip goodendsound;
    107.  
    108.     public AudioClip plus1up;
    109.  
    110.     private bool badsound;
    111.  
    112.     void Start()
    113.     {
    114.         audioSource = GetComponent<AudioSource>();
    115.         lives = 0;
    116.         score = 0;
    117.  
    118.         endstate = 0;
    119.  
    120.      
    121.         level = 0;
    122.         position = 0;
    123.  
    124.         isplay = false;
    125.         itstime = false;
    126.         canspawn = false;
    127.  
    128.         hiscore = PlayerPrefs.GetInt("hiscore");
    129.  
    130.         // Visualisation
    131.  
    132.         tempRestart.SetActive(false);
    133.         tempStart.SetActive(true);
    134.         tempTheEnd.SetActive(false);
    135.  
    136.         // Control
    137.  
    138.         touchbigbutton.SetActive(true);
    139.  
    140.  
    141.         // Objects
    142.  
    143.         ships = GameObject.FindGameObjectsWithTag("ship");
    144.         for (int i = 0; i < ships.Length; i++)
    145.         {
    146.             ships[i].SetActive(false);
    147.         }
    148.  
    149.         indicators = GameObject.FindGameObjectsWithTag("indicator");
    150.         for (int i = 0; i < indicators.Length; i++)
    151.         {
    152.             indicators[i].SetActive(false);
    153.         }
    154.  
    155.         lasers = GameObject.FindGameObjectsWithTag("laser");
    156.         for (int i = 0; i < lasers.Length; i++)
    157.         {
    158.             lasers[i].SetActive(false);
    159.         }
    160.  
    161.         threatlevel = GameObject.FindGameObjectsWithTag("threat");
    162.         for (int i = 0; i < threatlevel.Length; i++)
    163.         {
    164.             threatlevel[i].SetActive(false);
    165.         }
    166.  
    167.         badend = GameObject.FindGameObjectsWithTag("badend");
    168.         for (int i = 0; i < badend.Length; i++)
    169.         {
    170.             badend[i].SetActive(false);
    171.         }
    172.  
    173.         goodend = GameObject.FindGameObjectsWithTag("goodend");
    174.         for (int i = 0; i < goodend.Length; i++)
    175.         {
    176.             goodend[i].SetActive(false);
    177.         }
    178.  
    179.  
    180.         a0 = GameObject.FindGameObjectsWithTag("a0");
    181.         a1 = GameObject.FindGameObjectsWithTag("a1");
    182.         a2 = GameObject.FindGameObjectsWithTag("a2");
    183.         a3 = GameObject.FindGameObjectsWithTag("a3");
    184.         a4 = GameObject.FindGameObjectsWithTag("a4");
    185.         a5 = GameObject.FindGameObjectsWithTag("a5");
    186.  
    187.         e0 = GameObject.FindGameObjectsWithTag("e0");
    188.         e1 = GameObject.FindGameObjectsWithTag("e1");
    189.         e2 = GameObject.FindGameObjectsWithTag("e2");
    190.         e3 = GameObject.FindGameObjectsWithTag("e3");
    191.         e4 = GameObject.FindGameObjectsWithTag("e4");
    192.         e5 = GameObject.FindGameObjectsWithTag("e5");
    193.  
    194.         explotions = new List<GameObject[]>();
    195.         explotions.Add(e0);
    196.         explotions.Add(e1);
    197.         explotions.Add(e2);
    198.         explotions.Add(e3);
    199.         explotions.Add(e4);
    200.         explotions.Add(e5);
    201.  
    202.  
    203.         DeactivateAsteroids();
    204.         DeactivateExplotions();
    205.  
    206.     }

    Even a very simple control (add or subtract position number) often works randomly (but only in the browser)

    Code (CSharp):
    1.  // Touch controls
    2.  
    3.     public void Plus()
    4.     {
    5.         if (canmove)
    6.         {
    7.             if (position != 5)
    8.             {
    9.                 position += 1;
    10.             }
    11.             else
    12.             {
    13.                 position = 0;
    14.             }
    15.         }
    16.     }
    17.  
    18.     public void Minus()
    19.     {
    20.         if (canmove)
    21.         {
    22.             if (position != 0)
    23.             {
    24.                 position -= 1;
    25.             }
    26.             else
    27.             {
    28.                 position = 5;
    29.             }
    30.         }
    31.     }
     
  4. ivan_uspenskiy

    ivan_uspenskiy

    Joined:
    May 14, 2019
    Posts:
    8
    Oh, and this is step code:

    Code (CSharp):
    1.  void Update()
    2.     {
    3.        
    4.         if (isplay)
    5.         {
    6.  
    7.         ...
    8.             timer -= Time.deltaTime;
    9.  
    10.             Lifes();
    11.  
    12.             if (lives == 0)
    13.             {
    14.                 GameOver();
    15.             }
    16.  
    17.             if (timer <= 0)
    18.             {
    19.                 itstime = true;
    20. ...
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,899
    I think this may be a problem, in any case it's an issue prone to happen:
    Code (CSharp):
    1. e0 = GameObject.FindGameObjectsWithTag("e0");
    This will return a list of objects but like many such operations it probably doesn't guarantee a stable order of the resulting items. Unfortunately the docs don't say so, but I personally wouldn't expect it to return the objects in the same order at all times under all circumstances on all platforms and build configurations (debug/release).

    I would suggest making a webgl dev build with all debug features enabled (exception handling etc) and add Debug.Log where appropriate so you don't have to guess what's going on. Check the browser console for log messages.

    Above that, I wouldn't be surprised that the code just does a very good job at hiding further and future problems because this "GameManager" should actually be called "GameEverythingEverywhereAllAtOnce". :D
    The GameManager does too much, knows too much, thus anything can contribute to everything which will cause further issues and in the end a lot of blood, sweat and tears and eventually getting abandoned because the code is unmaintainable.
     
    ivan_uspenskiy likes this.
  6. ivan_uspenskiy

    ivan_uspenskiy

    Joined:
    May 14, 2019
    Posts:
    8
    Oh! A few great ideas! Thank you so much! Will try this ways and will post a result..
     
  7. ivan_uspenskiy

    ivan_uspenskiy

    Joined:
    May 14, 2019
    Posts:
    8
    You are the great! :) Thank you! It works now (after I changed the arrays from automatically collected to manual).
    ...
    But - only in local browser now. Unity Play don't want to go after 90%.
    I have already read a lot of things about this problem - but so far nothing has helped (about compression).
    I keep figuring out...
     
  8. ivan_uspenskiy

    ivan_uspenskiy

    Joined:
    May 14, 2019
    Posts:
    8