Browser -> Unity communication.

I’ve tried the following Javascript code in the browser to communicate with the Unity file…

document.getElementById(“unityPlayer”).SendMessage(“GameObject”, “WebAsset”, “testing!!”);

GetUnity().SendMessage(“GameObject”, “WebAsset”, “testing!!”);

GetUnity().SendMessage(“WebAsset”, “testing!!”);

But I’m not getting the “testing!!” string in Unity itself. What am I doing wrong?

It’s very basic code… I execute the assetpath() function from inside Unity and that works fine. But I never get the “testing!!” string. Anyone have any ideas?

<script type="text/javascript" src="http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject.js"></script>
<script type="text/javascript" language="javascript">
	function GetUnity() {
		if (typeof unityObject != "undefined") {
			return unityObject.getObjectById("unityPlayer");
		}
		return null;
	}
	
	if (typeof unityObject != "undefined") {
		unityObject.embedUnity("unityPlayer", "WebPlayer/WebPlayer.unity3d", 760, 456);
	}
	
	function assetpath() {
		alert("came to assetpath START");
		document.getElementById("unityPlayer").SendMessage("GameObject", "WebAsset", "testing!!");
//		GetUnity().Facebook.SendMessage("ActivateGame", "");
//		alert("GetUnity():"+GetUnity());
		<?php //echo 'the helloworld from php'; ?>
//		GetUnity().SendMessage("GameObject", "WebAsset", "testing!!");
//		GetUnity().SendMessage("WebAsset", "testing!!");
		alert("came to assetpath END");
	}
	
</script>

<p class="header"><span>Unity Web Player | </span>WebPlayer</p>
	<div class="content">
		<div id="unityPlayer">
			<div class="missing">
				<a href="http://unity3d.com/webplayer/" title="Unity Web Player. Install now!">
					<img alt="Unity Web Player. Install now!" src="http://webplayer.unity3d.com/installation/getunity.png" width="193" height="63" />
				</a>
			</div>
		</div>
	</div>
<p class="footer">« created with <a href="http://unity3d.com/unity/" title="Go to unity3d.com">Unity</a> »</p>

Going by the original (first 2 lines):

  1. You are sure the player is done loading when you call it?
  2. you have a game object called GameObject in the scene you call this
  3. That GameObject has some monobehaviour on it which has a function called WebAsset

Thank you! Reason 1 was the problem. :slight_smile: Regarding reason 0, how can I know if the player is done loading?

Easiest way to solve reason 0: make a monobehaviour that calls out in Awake in the first scene to let the page know that it is there.

that way you can move that object to later scenes if required, if you make scene 0 a movie intro in a streamed webplayer, where the website shouldn’t call in before you are in the real game for example

Thank you for the reply! :slight_smile: