Greeting! I need to call a javascript function from a C# class. How can I do it? I try the following: Code (csharp): 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!
This is what I did (in the attechment). javascript(in a file called "callee") Code (csharp): function Update () { } public function testFunc(a:String) { Debug.Log(a); } C# Code (csharp): 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!
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: http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html --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# Code (csharp): 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) Code (csharp): function Update () { } public function testFunc(a:String) { Debug.Log(a); } Thanks a lot!
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)
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.