how does C# call javascript?

Greeting!
I need to call a javascript function from a C# class. How can I do it?

I try the following:

private Javascript goScript; 
goScript = (Javascript ) gameObject.GetComponent("function name");

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

Try private scriptName. I think that should do it in most cases, but if it doesn’t, you can use GameObject.SendMessage(“FunctionName”, variable);

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!

Did you do private scriptName variableName or just scriptName?

This is what I did (in the attechment).

javascript(in a file called “callee”)

function Update () 
{
}


public function testFunc(a:String)
{
	Debug.Log(a);
}

C#

using UnityEngine;
using System.Collections;

public class caller : MonoBehaviour {
	private callee goScript;

	// Use this for initialization
	void Start () 
	{
		goScript = (callee) gameObject.GetComponent("testFunc"); 
	}
	
	// Update is called once per frame
	void Update () 
	{
	
	}
}

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!

210109–7721–$assets_271.zip (3.05 KB)

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.

Read this: Unity - Scripting API:

–Eric

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#

using UnityEngine;
using System.Collections;

public class caller : MonoBehaviour {
	private callee goScript;

	// Use this for initialization
	void Start () 
	{
		goScript = (callee) gameObject.GetComponent("callee"); 
	}
	
	// Update is called once per frame
	void Update () 
	{
		goScript.testFunc("1234");
	}
}

callee.js (in “Standard Assets” directory)

function Update () 
{
}


public function testFunc(a:String)
{
	Debug.Log(a);
}

Thanks a lot!

210120–7722–$assets_718.zip (3.22 KB)

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)

i have the same problem using unity iphone…

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.

project sample.

231842–8301–$testcallfunction_366.zip (1.07 MB)