Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Send Message from c# to javascript not being sended properly

Discussion in 'Scripting' started by klad, Jan 3, 2012.

  1. klad

    klad

    Joined:
    Jul 13, 2011
    Posts:
    17
    Hello,

    I'm having some problens trying to call a javascript function from a c# script.

    The c# script is placed under the Standard Assets folder ( because in addition, this c# script has to be accessed from outside, from other javascripts, this works perfeclty thought )

    The javascript that i'm trying to call is placed under Assets.

    The code is simply enough:

    C# script:

    GameObject ums = GameObject.Find( "UserManagerScript" );
    ums.SendMessage( "GetFriendsFromUserId", userID );

    Javascript file:

    function GetFriendsFromUserId( userID : int ) {
    print( "GetFriendsFromUserId: " + userID );
    ....
    }

    The print is never called, althought i do not get any compiler or execution errors. Any idea of what i may be doing wrong?

    Thanks in advance!
     
  2. Timbecile76

    Timbecile76

    Joined:
    Jul 8, 2009
    Posts:
    248
    Everything looks okay. Is the UserManagerScript a game object?

    You could try SendMessageUpwards or BroadcastMessage instead of SendMessage. That's what I usually do if I'm having this problem.

    I might also put a print right after the SendMessage and just double check that that like is being called also.
     
  3. klad

    klad

    Joined:
    Jul 13, 2011
    Posts:
    17
    I think i may have spotted where the problem is, but i dont know how to solve it.

    I shall explain a bit more the code i posted above, and tell you that the c# portion of code is inside a ReadCallback ( static, then ) of a socket,

    and i want to send the message from there. AND ( very important, i presume ), when i wrote this post, i simplified the javascript code for clearness, but in reality, there is a call to an WWW object before the print, and a yield w after that, and the print is not being called because is behind that yield, if i put a print before the yield it works correctly. My guess is i have to call StartCoroutine somewhere? I have no idea how coroutine works, but i think they may have something to help here ^^

    I post the complete code:

    c# code

    public static void ReadCallback(IAsyncResult ar)
    {
    String content = String.Empty;

    StateObject state = (StateObject)ar.AsyncState;
    Socket handler = state.workSocket;

    int bytesRead = handler.EndReceive(ar);

    GameObject ums = GameObject.Find( "UserManagerScript" );
    ums.BroadcastMessage( "GetFriendsFromUserId", state.userID );

    }


    javascript code



    function GetFriendsFromUserId( userID : int ) {

    print( "A" );
    var URL = "some_correct_address.php";
    var form = new WWWForm( );

    form.AddField( "id", "" + userID );


    var w = WWW(URL,form);
    print( "B" );
    yield w;

    print( "C" );


    As i said, print("C") is never reached, but "A" and "B" are. I am a bit confused about this, can someone give me some light on this?

    Thanks in advance!
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    var w = new WWW(URL, form);