Can't get SendMessage in webpage to send info to the web player

I am trying to get the containing webpage to send information to the unity web player with no luck.

I followed what they have here: http://docs.unity3d.com/Manual/UnityWebPlayerandbrowsercommunication.html

But I read other comments on this and found that people advise making a call to a function on the containing webpage from the web player through the start function, to make sure it’s loaded. Then I have that function execute SendMessage to send a text string back to the web player.

In the function (MyFunction) in Unity I just have it set a String variable to the text that is sent by SendMessage, then in OnGUI, I want it to display that value in a box, but I’m not able to get the value from SendMessage and assign it to the variable in my unity script.

Here is what I have:

In Unity:
myObjectScript.js ( this is attached to an object named MyObject)

#pragma strict

var myMessage : String = "testing..";

function Start ()
{
    Application.ExternalCall("DoSomething");
}

function OnGUI ()
{
    GUILayout.Box("myMessage: " + myMessage);
}


function MyFunction(param : String)
{
    myMessage = param;
}

In the html file:

var config = {
    width: 960,
    height: 600,
    params: { enableDebugging:"1" }
               
    };
    var u = new UnityObject2(config);

jQuery(function() {

    var $missingScreen = jQuery("#unityPlayer").find(".missing");
    var $brokenScreen = jQuery("#unityPlayer").find(".broken");
    $missingScreen.hide();
    $brokenScreen.hide();
               
    u.observeProgress(function (progress) {
        switch(progress.pluginStatus) {
            case "broken":
                $brokenScreen.find("a").click(function (e) {
                    e.stopPropagation();
                    e.preventDefault();
                    u.installPlugin();
                    return false;
                });
                $brokenScreen.show();
            break;
            case "missing":
                $missingScreen.find("a").click(function (e) {
                    e.stopPropagation();
                    e.preventDefault();
                    u.installPlugin();
                    return false;
                });
                $missingScreen.show();
            break;
            case "installed":
                $missingScreen.remove();
            break;
            case "first":
            break;
        }
    });
    u.initPlugin(jQuery("#unityPlayer")[0], "webcomm.unity3d");
    function DoSomething()
    {
             u.getUnity().SendMessage("MyObject", "MyFunction", "Hello!");
    }
});

Add an alert() in your html javascript code so you know DoSomething is called. Add a Debug.Log in your Unity script so you know MyFunction is called. Check both the browser JS error log, and the Unity error log.

Ok … I did what you suggested. I am not getting an alert box, and in my web console, it’s saying:

ReferenceError: DoSomething is not defined.

Is there somewhere else where this needs to be defined?

Ok I got it now. I had to move DoSomething() outside of jQuery(function() {

});

1 Like