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);
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" );
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");