Search Unity

how does C# call javascript?

Discussion in 'Scripting' started by Chien, Oct 17, 2009.

  1. Chien

    Chien

    Joined:
    May 1, 2009
    Posts:
    42
    Greeting!
    I need to call a javascript function from a C# class. How can I do it?

    I try the following:
    Code (csharp):
    1. private Javascript goScript;
    2. goScript = (Javascript ) gameObject.GetComponent("function name");
    3.  
    I guess the keyword Javascript isn't right. But don't know where to check the correct syntax. Could someone help me, and also tell me where is the place to search this type of question? Thanks!

    hch
    chien@storynest.com
     
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Try private scriptName. I think that should do it in most cases, but if it doesn't, you can use GameObject.SendMessage("FunctionName", variable);
     
  3. Chien

    Chien

    Joined:
    May 1, 2009
    Posts:
    42
    try that, but doesn't work.
    SendMessage works. But can I pass more than one variable in SendMessage? And I still want to know if C# call javascript is possible. Thanks for your help!
     
  4. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Did you do private scriptName variableName or just scriptName?
     
  5. Chien

    Chien

    Joined:
    May 1, 2009
    Posts:
    42
    This is what I did (in the attechment).

    javascript(in a file called "callee")
    Code (csharp):
    1.  
    2. function Update ()
    3. {
    4. }
    5.  
    6.  
    7. public function testFunc(a:String)
    8. {
    9.     Debug.Log(a);
    10. }
    C#
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class caller : MonoBehaviour {
    5.     private callee goScript;
    6.  
    7.     // Use this for initialization
    8.     void Start ()
    9.     {
    10.         goScript = (callee) gameObject.GetComponent("testFunc");
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update ()
    15.     {
    16.    
    17.     }
    18. }
    the error message said:
    Assets/caller.cs(5,17): error CS0246: The type or namespace name `callee' could not be found. Are you missing a using directive or an assembly reference?

    What would be the correct way to do this? Thanks!
     

    Attached Files:

  6. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Sometimes it doesn't recognize the JavaScript type unless it's put in a special folder. I forget the specifics, but you're pretty much stuck converting one or the other to the other language if you want easy communication.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  8. Chien

    Chien

    Joined:
    May 1, 2009
    Posts:
    42
    Hi!
    Thanks for your help! I solve it!

    Here is what I did:
    1. the javascript needs to put to a directory like "Standard Assets" or something like GargerathSunman Eric suggested to be compiled first.

    2. there are some minor bugs that I fixed.

    C#
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class caller : MonoBehaviour {
    5.     private callee goScript;
    6.  
    7.     // Use this for initialization
    8.     void Start ()
    9.     {
    10.         goScript = (callee) gameObject.GetComponent("callee");
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update ()
    15.     {
    16.         goScript.testFunc("1234");
    17.     }
    18. }
    callee.js (in "Standard Assets" directory)
    Code (csharp):
    1.  
    2. function Update ()
    3. {
    4. }
    5.  
    6.  
    7. public function testFunc(a:String)
    8. {
    9.     Debug.Log(a);
    10. }
    Thanks a lot!
     

    Attached Files:

  9. neverend

    neverend

    Joined:
    May 3, 2009
    Posts:
    9
    Your example still have a problem.please help me to solve it.

    NullReferenceException: Object reference not set to an instance of an object
    caller.Update () (at Assets/caller.cs:16)
     
  10. langju

    langju

    Joined:
    Jul 1, 2009
    Posts:
    207
    i have the same problem using unity iphone...
     
  11. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    then callee.js isn't in the right place (please follow, read and understand the page linked above) or you have not assigned it to any game object thats active in the current scene where you look for it.
     
  12. nan

    nan

    Joined:
    May 19, 2009
    Posts:
    10
    project sample.
     

    Attached Files: