jeffreylanters / react-unity-webgl

React Unity WebGL provides a modern solution for embedding Unity WebGL builds in your React Application while providing advanced APIs for two way communication and interaction between Unity and React.
https://react-unity-webgl.dev
Apache License 2.0
1.73k stars 163 forks source link

unityContext is undefined, Uncaught TypeError: Cannot read properties of undefined (reading 'unityConfig') #538

Closed zetachua closed 8 months ago

zetachua commented 8 months ago

Please avoid duplicates

Language and Compiler

Vanilla JavaScript

What environment are you using?

Local Development Server

When does your problem occur?

While writing code

What does your problem relate to?

The problem seems Module related

React-Unity-WebGL Version

9.5.0

React Version

18.2.0

Unity Version

2022.3.16f1

What happened?

I literally just pasted the getting started code and created a react project, my file structure is

react-unity-webgl --core --web ----public ------Build2 ----src ------app.jsx

its saying that unityContext is undefined... i have no idea what to do as ive looked thru all the forms and chatgpt...send help thanks

Reproducible test case

import './App.css' import { Unity, useUnityContext} from "react-unity-webgl";

function App() { const {unityContext} = useUnityContext({ loaderUrl: "/Build2/Build2.loader.js", dataUrl:"/Build2/Build2.data.unityweb", frameworkUrl:"/Build2/Build2.framework.js.unityweb", codeUrl: "/Build2/Build2.wasm.unityweb", }); console.log(unityContext,"meow") return ; }

export default App;

Would you be interested in contributing a fix?

jeffreylanters commented 8 months ago

You need to deconstruct unityProvider from the useUnityContext hook, you're trying to deconstruct unityContext instead - which is indeed not defined.

Either use

const unityContext = useUnityContext({

Or use

const { unityProvider } = useUnityContext({

zetachua commented 8 months ago

Hey Jeffrey! I just tried that it works! thank you:)