- Home /
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?
Edit: 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!\nId: " + result.id + "\nVersion: " + version);
//SentbottleColor();
}
else
{
alert("Please install Unity Web Player!");
}
}
function SentbottleColor()
{
alert("SetBottleColor start");
var unity = unityObject.getObjectById("UnityContent");
unity.SendMessage("Controller", "SetBottleColor", "2caffe");
alert("SetBottleColor end");
}
-->
</script>
Did you insert this piece of code into the HT$$anonymous$$L file generated by Unity during WebPlayer build?
Did you make sure that the unityObject was created before your piece of code?
No, I have a seperate html file (which is actually a copy of the generated file with extra stuff added). And yes, well actually, I thinks so, because unityObject.embedUnity() seems to work.
Answer by Shchvova · Jan 17, 2012 at 03:30 PM
Hey! 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
<script>
function UnityIsReady()
{
var unity = unityObject.getObjectById("UnityContent");
unity.SendMessage("MyObject", "MyFunction", "Hello from a web page!");
}
</script>
UPD2: I believe this is typo. Your IDs are different. Replace UnityContent
with unityPlayer
That's a good idea, but I only do the send$$anonymous$$essage when I press a button on the page, and loading should be long done by then
Lol... "First - ensure your Unity object have ID "UnityContent". It is not.
oh, ok, I didn't got what you meant. I didn't know the first parameter that was sent is the id you have to call later. the unityObject.embedUnity() part was already there when generated by Unity, I thought that first parameter was just the name of the webplayer or something. And I then looked at http://unity3d.com/support/documentation/$$anonymous$$anual/Unity%20Web%20Player%20and%20browser%20communication.html and they use a different name there (with is quiet misleading).
Thanks for the help, damn, I've wasted way too much on this.
Yes the documentation is sometimes misleading.
This one though is consistent from start to end: http://unity3d.com/support/documentation/$$anonymous$$anual/Working%20with%20UnityObject.html
Answer by Shchvova · Jan 17, 2012 at 09:12 PM
I bielieve your ids id different... Try var unity = unityObject.getObjectById("unityPlayer");
instead of var unity = unityObject.getObjectById("UnityContent");
Your answer
Follow this Question
Related Questions
Full screen not working from html message 0 Answers
How would I go about getting the results of a javascript variable to a string inside script? 3 Answers
Do joysticks only work in the desktop version??? 3 Answers
Why cant some users access my game using Chrome? 1 Answer
How can you detect in javascript when UnityObject2 fails to load the unity3d file? 0 Answers