Unity WebGL problem

unknown

hey when i want to call the LoginWithWeb3() with the HandleAuthButtonClick from the AppManager Script of the Tutorial then i get the error in the browser build. When I build the WebGL example of the youtube video, i have no problems. But in my game project the error occurs.

Here is the code I used für the HandAuthButtonCLick:


using UnityEngine;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using TMPro;

//Moralis
using MoralisWeb3ApiSdk;
using Moralis.WebGL;
using Moralis.WebGL.Platform.Objects;

#if UNITY_WEBGL
public class AppManager : MonoBehaviour
{
    public MoralisController moralisController;
    public GameObject authButton;
    public TextMeshProUGUI walletAddressLabel;

    private async void Start()
    {
        if (moralisController != null)
        {
            await moralisController.Initialize();
        }
        else
        {
            Debug.LogError("MoralisController is null.");
            return;
        }
      
        authButton.SetActive(!MoralisInterface.IsLoggedIn());
    }

    public async UniTask LoginWithWeb3()
    {
        string userAddr = "";
        
        if (!Web3GL.IsConnected())
        {
            userAddr = await MoralisInterface.SetupWeb3();
        }
        else
        {
            userAddr = Web3GL.Account();
        }

        if (string.IsNullOrWhiteSpace(userAddr))
        {
            Debug.LogError("Could not login or fetch account from web3.");
        }
        else
        {
            string address = Web3GL.Account().ToLower();
            string appId = MoralisInterface.GetClient().ApplicationId;
            long serverTime = 0;

            // Retrieve server time from Moralis Server for message signature
            Dictionary<string, object> serverTimeResponse = await MoralisInterface.GetClient().Cloud.RunAsync<Dictionary<string, object>>("getServerTime", new Dictionary<string, object>());

            if (serverTimeResponse == null || !serverTimeResponse.ContainsKey("dateTime") ||
                !long.TryParse(serverTimeResponse["dateTime"].ToString(), out serverTime))
            {
                Debug.Log("Failed to retrieve server time from Moralis Server!");
            }

            string signMessage = $"Moralis Authentication\n\nId: {appId}:{serverTime}";

            string signature = await Web3GL.Sign(signMessage);

            Debug.Log($"Signature {signature} for {address} was returned.");

            // Create moralis auth data from message signing response.
            Dictionary<string, object> authData = new Dictionary<string, object> { { "id", address }, { "signature", signature }, { "data", signMessage } };

            Debug.Log("Logging in user.");

            // Attempt to login user.
            MoralisUser user = await MoralisInterface.LogInAsync(authData);

            if (user != null)
            {
                Debug.Log($"User {user.username} logged in successfully. ");
                
                authButton.SetActive(false);
                walletAddressLabel.gameObject.SetActive(true);
                walletAddressLabel.text = GetWalletAddress(user);
            }
            else
            {
                Debug.Log("User login failed.");
            }
        }
    }
    
    private string GetWalletAddress(MoralisUser user)
    {
        return user.ethAddress;
    }
    
    public async void HandleAuthButtonClick()
    {
        await LoginWithWeb3();
    }
}
#endif
1 Like

Here is more info of the error:

Uncaught TypeError: Cannot read properties of undefined (reading 'connect')
    at _ConnectWeb3 (build.framework.js.gz:2:44131)
    at build.wasm.gz:0x14ad2f3
    at build.wasm.gz:0x27702b8
    at Object.dynCall_viiii (build.framework.js.gz:2:526836)
    at invoke_viiii (build.framework.js.gz:2:396371)
    at build.wasm.gz:0x24f97c0
    at build.wasm.gz:0x14ad5b9
    at build.wasm.gz:0x277028a
    at Object.dynCall_viii (build.framework.js.gz:2:525363)
    at invoke_viii (build.framework.js.gz:2:393710)
    at build.wasm.gz:0x16957b6
    at build.wasm.gz:0x158c906
    at build.wasm.gz:0x2770210
    at Object.dynCall_vii (build.framework.js.gz:2:522649)
    at invoke_vii (build.framework.js.gz:2:388773)
    at build.wasm.gz:0x141c109
    at build.wasm.gz:0x110d1a9
    at build.wasm.gz:0x162030a
    at build.wasm.gz:0x277028a
    at Object.dynCall_viii (build.framework.js.gz:2:525363)
    at invoke_viii (build.framework.js.gz:2:393710)
    at build.wasm.gz:0x141b9ec
    at build.wasm.gz:0x2770210
    at Object.dynCall_vii (build.framework.js.gz:2:522649)
    at invoke_vii (build.framework.js.gz:2:388773)
    at build.wasm.gz:0x16abeb9
    at build.wasm.gz:0x1620542
    at build.wasm.gz:0x1620531
    at build.wasm.gz:0xee298a
    at build.wasm.gz:0x1d58aa0
    at build.wasm.gz:0x1f8a832
    at build.wasm.gz:0x1ba948f
    at build.wasm.gz:0x1ba94a5
    at build.wasm.gz:0xca8ded
    at build.wasm.gz:0x1bb59f0
    at build.wasm.gz:0xf223ff
    at build.wasm.gz:0x27702b8
    at Object.dynCall_viiii (build.framework.js.gz:2:526836)
    at invoke_viiii (build.framework.js.gz:2:396371)
    at build.wasm.gz:0x124284b
    at build.wasm.gz:0x1ba8c3f
    at build.wasm.gz:0x25fbac7
    at build.wasm.gz:0x25fd141
    at build.wasm.gz:0x25fc9c8
    at build.wasm.gz:0x25fc31b
    at build.wasm.gz:0x25fbffb
    at build.wasm.gz:0xca7a59
    at build.wasm.gz:0x1bb52a3
    at build.wasm.gz:0xffd6b8
    at build.wasm.gz:0x276fd81

Hey mate, facing the same issue. Did you manage to find the solution?

Hey guys, for everybody who will face this issue - this might be a fix:

You have to import moralis WebGLTemplate. It has defined some js functions and libs. Without them, you will receive the error.

  1. In your Assets folder create a folder named WebGLTemplates.
  2. Go to Packages/MoralisWeb3UnitySDK/Resources/WebGLTempalte and copy the MoralisWebGL folder to the one you created above.
  3. Go to the Top Menu -> File -> Build Settings -> Player Settings -> Resolution and choose the Moralis WebGL template
  4. Build and Run.

Worked for me. I hope this will save you time.

1 Like