Unity Community


Results 1 to 5 of 5

  1. Location
    UK
    Posts
    31

    Calling a C# function from javascript

    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/documenta...dvanced29.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:  
    1. private var seeker = new Seeker();
    2. function MoveSelectedUnitsToPoint(destinationPoint : Vector3)
    3. {
    4.  
    5.     for(var go : GameObject in selectedUnitsList)
    6.     {
    7.         if(debugMode)
    8.         {
    9.             print("Moving units: " +go.name);
    10.         }
    11.         seeker.StartPath(go.transform.position, destinationPoint);
    12.  
    13.     }
    14. }
    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:  
    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:  
    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. Posts
    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:  
    1. function Start()
    2. {
    3. }
    4.  
    5. function Update ()
    6. {
    7.     if (Input.GetKey(KeyCode.End))
    8.     {
    9.         var cs = GameObject.Find("CSharpGameObj");
    10.         var script = cs.GetComponent("CSharp");
    11.         script.printTestCS();
    12.     }
    13. }
    14.  
    15. function printTestJS()
    16. {
    17.     Debug.Log("Inside JS");
    18. }

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

    Press Home or End to invoke the scripts


  3. Posts
    41

    Re: Calling a C# function from javascript

    Quote Originally Posted by Scoots
    Code:  
    1. private var seeker = new Seeker();
    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:  
    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. Location
    UK
    Posts
    31
    Hey thanks for the replys, i used a combination of both to get it working much apreciated
    Code:  
    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. Posts
    9
    Hi, another option is using SendMessage to call a C# Function from Javascript and vice versa.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •