- Home /
Unity/Browser com driving me crazy
I have been trying to use the examples provided with no success at all. http://docs.unity3d.com/Documentation/Manual/UnityWebPlayerandbrowsercommunication.html
I have followed this to the letter, not even getting the Application.ExternalCall to the browser.
I have found that the result of var u = new UnityObject2(); as specified in the link is that u is NULL. There is a note that that may happen if game is not fully loaded. So how to delay my call till the game is loaded. How can I tell that the player and assets are ready to go?
Where exactly are you calling this new UnityObject2()
code?
<script type="text/javascript">
swfobject.registerObject("unityPlayer", "11.2.0");
function kickit()
{
alert('GOT IT');
}
var u = new UnityObject2();
u.initPlugin(jQuery("#unityPlayer")[0], "Example.unity3d");
function SaySomethingToUnity()
{
if(u.getUnity() == null){
alert('IN Func and u is NULL');
}
//document.write("inFucncion");
u.getUnity().Send$$anonymous$$essage("$$anonymous$$yObject", "$$anonymous$$yFunction", "Hello from a web page!");
//u.getObjectById("UnityContent").Send$$anonymous$$essage("$$anonymous$$yObject", "$$anonymous$$yFunction", "Hello from web page");
//document.getElementById("unityPlayer").Send$$anonymous$$essage("$$anonymous$$yObject", "$$anonymous$$yFunction", "Hello from a web page!");
alert('PAST UNITY Call');
}
</script>
Thank you again for responding and taking an interest. Sorry for the delay, holiday time here.
I ran the test in Chrome and FireFox. In both cases I get "object, Object".
Answer by ArkaneX · Nov 27, 2013 at 10:12 PM
When you build your project with Web Player target, two files are in the output folder: xxxxx.html and xxxxx.unity3d. Please take a look at the content of html one - there are parts that must be included, in order for the whole stuff to work. Script used to dynamically include UnityObject2.js for example:
<script type="text/javascript">
<!--
var unityObjectUrl = "http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject2.js";
if (document.location.protocol == 'https:')
unityObjectUrl = unityObjectUrl.replace("http://", "https://ssl-");
document.write('<script type="text\/javascript" src="' + unityObjectUrl + '"><\/script>');
-->
</script>
Adding below code after above script should work properly. Adding it before will not, because UnityObject2 is not yet available.
<script>
var u = new UnityObject2();
alert(u);
</script>
Thank you so much for responding!! But sadly I am still getting "undefined" for u.
Please copy below script and paste it to an empty html file and then run it. Does alert show 'undefined' or '[object Object]'? Or maybe no alert is displayed and you have an error in browser console?
<html>
<head>
<script type='text/javascript' src='https://ssl-webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/jquery.$$anonymous$$.js'></script>
<script type="text/javascript">
<!--
var unityObjectUrl = "http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject2.js";
if (document.location.protocol == 'https:')
unityObjectUrl = unityObjectUrl.replace("http://", "https://ssl-");
document.write('<script type="text\/javascript" src="' + unityObjectUrl + '"><\/script>');
-->
</script>
<script>
var u = new UnityObject2();
alert(u);
</script>
</head>
</html>
EDIT: pleast test this in at least 2 browsers.
With this added insight I have cut out the style tags in the html Unity produces and I am getting at least "object, Object" now ins$$anonymous$$d of "undefined". And I am getting past the if (u==null) test in my button. So I have a Unity Object now. But I am not hitting or passing the call to Unity. I have found three possible ways of doing that in the forums but none seem to be passing.
u.getUnity().Send$$anonymous$$essage("$$anonymous$$yObject", "$$anonymous$$yFunction", "Hello from a web page!");
//u.getObjectById("UnityContent").Send$$anonymous$$essage("$$anonymous$$yObject", "$$anonymous$$yFunction", "Hello from web page");
//document.getElementById("unityPlayer").Send$$anonymous$$essage("$$anonymous$$yObject", "$$anonymous$$yFunction", "Hello from a web page!");
alert('PAST UNITY Call');
I have tried all three but never get to the last alert confir$$anonymous$$g I have gotten back from Unity.
Converted your answer to comment, as it doesn't answer the question...
As to your problem - I think you're calling your method just after setting u
variable, when Unity object is not fully loaded (`u.getUnity()` returns null
). This causes an error which you can see in browser console, breaking execution of further script. That's why alert
is not called.
To see that code from Unity page works, please add a function to your script, just as described in the documentation:
function SaySomethingToUnity()
{
u.getUnity().Send$$anonymous$$essage("$$anonymous$$yObject", "$$anonymous$$yFunction", "Hello from a web page!");
}
and then add a simple html to your code:
<b onclick='SaySomethingToUnity()'>click me</b>
and try to click this after your app is loaded. Be sure to have a game object named $$anonymous$$yObject in your scene, with a script attached containing a function $$anonymous$$yFunction accepting one String parameter. For me it works perfectly.
Thank you. I have done exactly that. In fact I have also tried ....
//u.getObjectById("unityPlayer").Send$$anonymous$$essage("$$anonymous$$yObject", "$$anonymous$$yFunction", "Hello from web page");
and //document.getElementById("unityPlayer").Send$$anonymous$$essage("$$anonymous$$yObject", "$$anonymous$$yFunction", "Hello from a web page!");
I also have Application.ExternalCall ("$$anonymous$$yFunction2", "Hello from Unity!"); in Unity at Start on an object calling the browser and getting nothing. Very frustrating.
Answer by diggerjohn · Dec 02, 2013 at 03:00 PM
After all of this something about your last suggestion/communication triggered the old brain cells. I have been trying to do this through the Flash build. I instead, this morning, tried a build of the Web Player and it all works as advertised. I wish I could do it through the Flash build though. Thank you all for sticking with me through this discovery process.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122855im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Passing variable into Unity function from browser JS via ActionScript 1 Answer
UnityObject callback function never runs 2 Answers
change player possition via url 0 Answers
Need help for simple moveforward function in Unity Web Player and browser communication 0 Answers
Can't get SendMessage in webpage to send info to the web player 1 Answer