unityObject is null

I’m trying to call a Unity function from the browser. (see http://unity3d.com/support/documentation/Manual/Unity%20Web%20Player%20and%20browser%20communication.html)

so I do this:

var unity = unityObject.getObjectById("UnityContent");
unity.SendMessage("Controller", "SetBottleColor", "2caffe");

but this doesn’t work because unityObject is null.

I have no idea what I’m dooing wrong,
I thought I might call this too soon, and the web player’s content isn’t loaded yet. But I’m now triggering this when I press a button on the page an it’s still the same problem.

Any help?

here’s the entire javascript part of the html file:

	<script type="text/javascript" src="http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject.js"></script>
	<script type="text/javascript">
	if (typeof unityObject != "undefined") {
		unityObject.embedUnity("unityPlayer", "WebPlayer.unity3d", 600, 450, null, null, unityLoaded);
	function unityLoaded(result) 
		if (result.success) {
			var unity = result.ref;
			var version = unity.GetUnityVersion("3.x.x");
			alert("Unity Web Player loaded!

Id: " + result.id + "
Version: " + version);
alert(“Please install Unity Web Player!”);
function SentbottleColor()
alert(“SetBottleColor start”);
var unity = unityObject.getObjectById(“UnityContent”);
unity.SendMessage(“Controller”, “SetBottleColor”, “2caffe”);
alert(“SetBottleColor end”);

There is couple things which can go wrong. First - ensure your Unity object have ID “UnityContent”.
Than, try to write your code in browser console (Ctrl+Shift+J in Firefox and Chrome)
line by line. If it will work, than problem is that you calling your code while object is not created yet.

UPD: You can not access unity object just before it was created. You should wait while it is loaded and ready to accept messages. I’d recommend to determine it by first sending message to browser javascript. For example
In Unity do something like this:
Application.ExternalCall( "UnityIsReady"); in Start() function
and in web page javascript write something like

function UnityIsReady()
	var unity = unityObject.getObjectById("UnityContent");
	unity.SendMessage("MyObject", "MyFunction", "Hello from a web page!");


I believe this is typo. Your IDs are different. Replace UnityContent with unityPlayer

I bielieve your ids id different…
Try var unity = unityObject.getObjectById("unityPlayer");
instead of var unity = unityObject.getObjectById("UnityContent");