Before updating to Windows 10 you may need to return your Pro license. More information here

call function in javascript from c# script

How would I call a function in a js script attached to a GameObject from a c# script. the JSscript is attached to a GameObject named Cube. I'm trying to do this so far in the C# script but it's not working. I know how to do this from Js to Js but not C# to Js. Here's my non-working code attempt.

 public GameObject go;
 public JSscript myscript;
 void Start(){

right now it doesn't know what JSscript is.

more ▼

asked Oct 10, 2012 at 07:07 PM

avatar image

617 471 381 424

The c# script is a plugin so it's already in the plugins folder. JS needs to be compiled first but plugins are compiled first. I can't use SendMessage in this case. It's too slow. I tried changing the editor script order preference by the compiler still doesn't know what JSscript is. It says the type of namespace cannot be found.

Oct 10, 2012 at 07:35 PM dansav

If the c# file is already in the plugins folder, you're out of luck. C# and js can't call each other at the same level; one of them needs to be compiled first (by being in the plugins folder). Why can't you move the c# script into your assets folder?

Oct 10, 2012 at 07:51 PM kmeboe

If you can't move the c#, I wonder if you could have it call a different c# script that is in your assets folder, and then that one could call the js method. Worth a try.

Oct 10, 2012 at 07:52 PM kmeboe

I tried moving things around but the error never changes. Is there another way besides sendmessage to call a script. Maybe something that wouldn't depend on the compilation order?

Oct 10, 2012 at 08:05 PM dansav

If you put your c# file directly in assets, and your js file in Assets/Plugins, it should work; I just successfully reproduced this on my machine.

Note that MonoDevelop will give you errors if you try to build from there, but everything works perfectly in Unity.

Here's my code. MyRegularFunc is in the javascript file "testjs.js", and it outputs to the log when it's called.

     GameObject found = GameObject.Find ("Sphere");
     if (found)
         var foundScript = found.GetComponent<testjs>() as testjs;
         if (foundScript)
         else Debug.LogError("No attached testjs script.");
     else Debug.LogError("No Sphere");
Oct 10, 2012 at 08:28 PM kmeboe
show all comments (comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

This answer will tell you what you want to know: http://answers.unity3d.com/questions/267475/calling-javascript-method-fron-c-ngui.html

Summarizing: the javascript file would need to be compiled first (by putting it in one of the referenced directories). An alternative would be to use SendMessage().

more ▼

answered Oct 10, 2012 at 07:26 PM

avatar image

1.5k 7 14 20

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Oct 10, 2012 at 07:07 PM

Seen: 8957 times

Last Updated: Oct 10, 2012 at 08:45 PM