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 Unity - Scripting API: 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.

private var seeker = new Seeker();
function MoveSelectedUnitsToPoint(destinationPoint : Vector3)
{

	for(var go : GameObject in selectedUnitsList)
	{
		if(debugMode)
		{
			print("Moving units: " +go.name);
		}
		seeker.StartPath(go.transform.position, destinationPoint);

	}
}

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 atseeker.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

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.

I did this creating two empty objects and asigning the scripts to them. Here´s the code for the JavaScript (called JavaS.js)

function Start()
{
}

function Update () 
{
	if (Input.GetKey(KeyCode.End))
	{
		var cs = GameObject.Find("CSharpGameObj");
		var script = cs.GetComponent("CSharp");
		script.printTestCS();
	}
}

function printTestJS()
{
	Debug.Log("Inside JS");
}

The other script is called CSharp.cs

using UnityEngine;
using System.Collections;

public class CSharp : MonoBehaviour 
{	
	void Start () 
	{
	}
	
	void Update () 
	{
		if (Input.GetKey(KeyCode.Home))
		{
			GameObject jj = GameObject.FindWithTag("jsgobj"); 
			
			if (jj == null)
			{
				Debug.Log("Error, null JavaScript Component");
			}
			else
			{
				(jj.GetComponent(typeof(JavaS)) as JavaS).printTestJS();
			}
		}
	}
	
	public void printTestCS()
	{
		
		Debug.Log("Inside C#");
	}
}

Press Home or End to invoke the scripts

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

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).

Hey thanks for the replys, i used a combination of both to get it working :slight_smile: much apreciated

function MoveSelectedUnitsToPoint(destinationPoint : Vector3)
{

	for(var go : GameObject in selectedUnitsList)
	{
		if(debugMode)
		{
			print("Moving units: " +go.name);
		}
		var seeker = go.GetComponent("Seeker");
		seeker.StartPath(go.transform.position, destinationPoint);	} 
}

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 :slight_smile:
hopefully others can make use of this if they come across it.

Cheers again

Hi, another option is using SendMessage to call a C# Function from Javascript and vice versa.