- Home /
I can't get browser to Webgl communciation working
Application.ExternalCall()
for communicating from the Webgl player to the browser works perfectly. But using SendMessage ('MyGameObject', 'MyFunction', 'foobar');
as described in the docs doesn't work. I end up getting the following error:
"An error occured running the Unity content on this page. See your browser's JavaScript console for more info. The error was: ReferenceError: SendMessage is not defined"
I'm calling these two functions after <script src="Release/UnityLoader.js"></script>
in the index.html file that comes with Webgl build folder
<script>
SessionTokenToUnity();
LevelUrlIdToUnity();
</script>
And they are being defined at the top of the UnityLoader.js file like so:
function SessionTokenToUnity(token) {
SendMessage("GameControl", "ReceiveToken", token);
}
function LevelUrlIdToUnity(urlId) {
SendMessage("GameControl", "ReceiveUrlId", urlId);
}
Answer by tborja · Dec 20, 2015 at 03:17 PM
The most likely problem is that your Unity content is not completely loaded yet when you are calling your script. Try wrapping your script in another function that you call from the GameControl Monobehavior's Start event.
<script>
function gameControlReady() {
SendMessage("GameControl", "MyFunction", message);
};
</script>
In your GameControl Monobehavior, you then call the method using ExternalCall() like so.
void Start() {
Application.ExternalCall("gameControlReady");
};
Answer by dentedpixel · Nov 05, 2017 at 11:56 AM
For me it was fixed by using SendMessage on the instance that is returned from the Unity Loader (which they don't make clear in the documentation). You will need to do something like this:
var gameInstance = UnityLoader.instantiate("gameContainer", "Build/webgl.json");
// later once game has loaded
gameInstance.SendMessage("MainCamera", "initializeData", "data" );
thank you so much for posting this! (worked) (been stuck on this since last night)
Your answer
Follow this Question
Related Questions
Unity WebGL calling a function from a jslib file in JavaScript(Vue) 0 Answers
WebGL: Interacting with browser scripting unable to call java script method from C# 2 Answers
Interact across browser javascipt and unity webgl 2 Answers
How to send a large string from Javascript to WebGL ? 0 Answers
Is there a way to send an XMLHttpRequest and get the response in a WWW object? 0 Answers