Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unity 2021.2b6 Uncaught DOMException - Canvas requires ID now #223

Closed
KamilCSPS opened this issue Aug 5, 2021 · 25 comments
Closed

Unity 2021.2b6 Uncaught DOMException - Canvas requires ID now #223

KamilCSPS opened this issue Aug 5, 2021 · 25 comments
Assignees
Labels
needs more info Needs more info needs testing Needs testing react related Ticket is React related

Comments

@KamilCSPS
Copy link

KamilCSPS commented Aug 5, 2021

Describe the bug
"Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '#'" is thrown when loading a build. No errors are thrown when using default Unity WebGL template with index.html.

To Reproduce
Steps to reproduce the behavior:

  1. Build empty WebGL build in a react-unity app.
  2. Notice WebGL build not loading with "Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '#'" error.
  3. Add ID to canvas, reload.
  4. Build works with ID on canvas.

Expected behavior
Same content built with Unity 2021.1f14 works seamlessly with same setup.

Screenshots
N/A

Details (please complete the following information):

  • React Unity WebGL Version: 8.4.9
  • Device and OS: Win10
  • Browser: Chrome 92.0.4515.107
  • NodeJS Version: 12
  • Bundler: Webpack 5.5.0

Additional context
N/A

@romy-piche
Copy link

I think I found the issue: Unity seems to have changed the way to get the canvas in the 2021.2 version, and now uses the canvas id element. Since the canvas created by React Unity WebGL has no id element, it tries to find it with an empty id using querySelector, which is invalid:
document.querySelector("#")
I was able to recreate the same error by removing the id element of the canvas in the html file generated by Unity.

@jeffreylanters
Copy link
Owner

Thanks for doing some research. And oh well... once again Unity breaks existing implementations. I see this version of Unity seems to be still in beta. I'll look into this. Thanks again for reporting and your research.

@jeffreylanters
Copy link
Owner

Update: It's not documented yet in their Unity 2021.2 documentation. Therefor I will wait with the upgrade until this Unity version is out of beta.

@KamilCSPS KamilCSPS changed the title Unity 2021.2b6 Uncaught DOMException with anchor # Unity 2021.2b6 Uncaught DOMException - Canvas requires ID now Aug 9, 2021
@jeffreylanters jeffreylanters added react related Ticket is React related needs more info Needs more info needs testing Needs testing TAKEN labels Aug 9, 2021
@jeffreylanters jeffreylanters self-assigned this Aug 9, 2021
@github-actions
Copy link

github-actions bot commented Sep 9, 2021

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Inactive for too long label Sep 9, 2021
@jeffreylanters jeffreylanters removed the stale Inactive for too long label Sep 9, 2021
@github-actions
Copy link

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Inactive for too long label Oct 10, 2021
@jeffreylanters jeffreylanters removed the stale Inactive for too long label Oct 10, 2021
@SaIG
Copy link

SaIG commented Oct 27, 2021

2021.2 is now out of beta ... and just ran into this issue ... is there a way for quick and dirty fix? Adding just a canvas id raised just more issues ^^

@jeffreylanters
Copy link
Owner

jeffreylanters commented Oct 27, 2021

What a poor decision by Unity. I'm working on a big release including lots of new features and a complete functional rewrite. I will add support for this as well, I'm available tomorrow and will try to push this version out as soon as possible. I'll keep you posted on this thread.

@jeffreylanters
Copy link
Owner

jeffreylanters commented Oct 28, 2021

@SaIG @CarpetHead @kamilda I've installed Unity 2021.2.0f1 and made a build of a new empty project. But the createUnityInstance still takes a Canvas Element rather than an Element id. Am I missing something...?

Screenshots from the generated Unity default WebGL template:
image
image

@jeffreylanters
Copy link
Owner

Running a Unity 2021.2.0f1 build using an Element reference instead of an ID. Can be confirmed by the updated splash screen.

image

@SaIG
Copy link

SaIG commented Oct 28, 2021

@jeffreylanters Okay thx 4 checkin & sry for the hassle then ... i have to check my implementation .. its a bit strange because the only thing i changed is switching 2021.1 to 2021.2 version. Let me reevalute this ...

Repository owner deleted a comment from SaIG Oct 28, 2021
@CarpetHead
Copy link

It's not the constructor, it's something deep within the build JavaScript. The stack trace of the error suggests it comes from emscripten. I honestly have no idea what the cause of the issue is, but 2021.2 has this issue and 2021.1 does not. Are you unable to reproduce it when using react-unity?

@jeffreylanters
Copy link
Owner

Hm. Yup, I've installed the latest fresh release op Unity 2021.2, made a WebGL build and tried it within react-unity-webgl. Everything runs as expected. Also when looking at the actual Unity build I notice that they also still use a canvas reference rather than a ID. So I'm not able to reproduce this.

@CarpetHead
Copy link

my error logs are very input related, perhaps try enabling the new input backend?
image
(Active Input Handling)

@CarpetHead
Copy link

if it is of any value, this is the full stacktrace of the issue

image

@CarpetHead
Copy link

modifying unity.js to this fixes it:

    return react_1.createElement("canvas", {
        id: "unity-canvas",
        ref: function (ref) { return (_this.htmlCanvasElementReference = ref); },
        className: this.props.className || "",
        tabIndex: this.props.tabIndex || undefined,
        style: this.props.style || {},
    });

@jeffreylanters
Copy link
Owner

jeffreylanters commented Oct 29, 2021

I am completely lost. I can try enabling the new input system, but this still does not make any sense regarding the element id. On what is the ID "unity-canvas" based? The module supports having multiple Unity Instances at once. Could you try building an empty Unity project to see if the issue is project (config) or Unity package manager dependency related?

@CarpetHead
Copy link

I just copied that ID directly from the template files that unity provides alongside a build. Perhaps you could make the id match the class name? the fix works perfectly for me, although I have no idea how I would go about actually fixing it in a way that will reach our build server?

@jeffreylanters
Copy link
Owner

It might just require AN id to be used internally within the Unity framework. I can add a unique id per canvas to prevent this error from occurring. For now this hack should prevent the error from occurring as well:

import React, { useEffect, useState } from "react";
import Unity, { UnityContext } from "react-unity-webgl";

const unityContext = new UnityContext({
  loaderUrl: "build/myunityapp.loader.js",
  dataUrl: "build/myunityapp.data",
  frameworkUrl: "build/myunityapp.framework.js",
  codeUrl: "build/myunityapp.wasm",
});

function App() {
  useEffect(function () {
    unityContext.on("canvas", function (canvas) {
      canvas.id = "unity-canvas";
    });
  }, []);

  return <Unity unityContext={unityContext} />
}

@CarpetHead
Copy link

CarpetHead commented Oct 29, 2021

I actually tried that first, but it looks like the error happens before the callback, so that hack didn't work for me. Let me have another try

@jeffreylanters
Copy link
Owner

jeffreylanters commented Oct 29, 2021

Ah, sorry I did not try it before posting. I can release a hot fix which adds a unique identifier for now while I work on the recode.

@CarpetHead
Copy link

if possible that would be perfect, can test it whenever you have something

@CarpetHead
Copy link

looks like it also works with a random id - "asdf" worked perfectly

@jeffreylanters
Copy link
Owner

I've just published a patch release version 8.5.2 which includes a unique identifier for each Unity Canvas instance. Could you try this?

@CarpetHead
Copy link

works :)

@jeffreylanters
Copy link
Owner

Thank you so much for helping and testing! If you run into other problems, please keep me posted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs more info Needs more info needs testing Needs testing react related Ticket is React related
Projects
None yet
Development

No branches or pull requests

5 participants