Calling a C# function from javascript

Discussion in 'Scripting' started by Scoots, Nov 9, 2009.

  1. Scoots

    Scoots

    New Member

    Joined:
    Oct 21, 2009
    Messages:
    31
    Hi all, first off, i'm trying to call a C# function from my Javascript, however im a little confused on how to go about it.

    Ive read up on http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html and fully understand how the conversions work, I have my C# scripts placed in my Standard Assets folder and my javascript files are in the last to be compiled folder.

    In my Javascript i am trying to call the C# function StartPath which is part of the Seeker script which is attatched to each my tank/s gameobject/s.

    Code (csharp):
    1.  
    2. private var seeker = new Seeker();
    3. function MoveSelectedUnitsToPoint(destinationPoint : Vector3)
    4. {
    5.  
    6.     for(var go : GameObject in selectedUnitsList)
    7.     {
    8.         if(debugMode)
    9.         {
    10.             print("Moving units: " +go.name);
    11.         }
    12.         seeker.StartPath(go.transform.position, destinationPoint);
    13.  
    14.     }
    15. }
    Thats the javascript function that is used to call the C# function.

    If i run the game , and call the function i get the error: NullReferenceException, and it points me at
    Code (csharp):
    1. seeker.StartPath(go.transform.position, destinationPoint);
    additionaly there is alot of these warnings
    You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all.
    all pointing at the line
    Code (csharp):
    1. private var seeker = new Seeker();
    Have i missed something key out here? Only ive searched the forums and havent had much luck with what i have tried so far.

    Cheers.
  2. ocuspocus

    ocuspocus

    New Member

    Joined:
    Jul 29, 2009
    Messages:
    13
    I did this creating two empty objects and asigning the scripts to them. HereĀ“s the code for the JavaScript (called JavaS.js)

    Code (csharp):
    1.  
    2. function Start()
    3. {
    4. }
    5.  
    6. function Update ()
    7. {
    8.     if (Input.GetKey(KeyCode.End))
    9.     {
    10.         var cs = GameObject.Find("CSharpGameObj");
    11.         var script = cs.GetComponent("CSharp");
    12.         script.printTestCS();
    13.     }
    14. }
    15.  
    16. function printTestJS()
    17. {
    18.     Debug.Log("Inside JS");
    19. }
    The other script is called CSharp.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CSharp : MonoBehaviour
    6. {  
    7.     void Start ()
    8.     {
    9.     }
    10.    
    11.     void Update ()
    12.     {
    13.         if (Input.GetKey(KeyCode.Home))
    14.         {
    15.             GameObject jj = GameObject.FindWithTag("jsgobj");
    16.            
    17.             if (jj == null)
    18.             {
    19.                 Debug.Log("Error, null JavaScript Component");
    20.             }
    21.             else
    22.             {
    23.                 (jj.GetComponent(typeof(JavaS)) as JavaS).printTestJS();
    24.             }
    25.         }
    26.     }
    27.    
    28.     public void printTestCS()
    29.     {
    30.        
    31.         Debug.Log("Inside C#");
    32.     }
    33. }
    34.  
    Press Home or End to invoke the scripts
  3. immortius

    immortius

    New Member

    Joined:
    Aug 7, 2009
    Messages:
    41
    In brief, you are trying to create a Seeker here, when instead you should be getting the existing Seekers on your Tanks. If your javascript component is attached to your Tanks, you should be able to do this by using

    Code (csharp):
    1. private var seeker = GetComponent(typeof(Seeker)) as Seeker;
    or something along those lines (I don't use javascript, sorry, so I don't know the exact syntax).
  4. Scoots

    Scoots

    New Member

    Joined:
    Oct 21, 2009
    Messages:
    31
    Hey thanks for the replys, i used a combination of both to get it working :) much apreciated
    Code (csharp):
    1. function MoveSelectedUnitsToPoint(destinationPoint : Vector3)
    2. {
    3.  
    4.     for(var go : GameObject in selectedUnitsList)
    5.     {
    6.         if(debugMode)
    7.         {
    8.             print("Moving units: " +go.name);
    9.         }
    10.         var seeker = go.GetComponent("Seeker");
    11.         seeker.StartPath(go.transform.position, destinationPoint)}
    12. }
    Theres a seeker script within each tank that can be selected, so i only had to get that component off the go variable, i think i also overcomplicated my situation, thinking there was alot of conversion required to use the c# function :)
    hopefully others can make use of this if they come across it.

    Cheers again
  5. srmojuze

    srmojuze

    Member

    Joined:
    Mar 18, 2013
    Messages:
    26
    Hi, another option is using SendMessage to call a C# Function from Javascript and vice versa.