Getting C# script to call a Javascript function

In Javascript, I have no problems getting scripts to talk to each other, but now I’ve got a function in a c# script, and I want it to call a function in a different Javascript, and I can’t, for the life of me figure out how to do it. No matter what I do, I keep getting “the type or namespace name “javascript script” could not be found…”

How am I supposed to do this?

The very first thing you always need to do with this is read very carefully through the following document. It talks abou the sequence that unity follows when executing scripts.

I did look at that, and I have both scripts in the same directory, and attached to the same gameObject.

So if I try to get the javascript component from the c# script using:

tools = (DisplayTools) GetComponent(typeof(DisplayTools);

I get "the type or namespace ‘DisplayTools’ Could not be found.

If I try to access the javascript static function using:

DisplayTools.Test();

I get “The Name ‘DisplayTools’ does not exist in the current context”

I know just about zero about writing in c#, so I’m flying a little blind here…

DisplayTools tools=GetComponent();
tools.Test();

I think that is the format for that.

Thanks B, but that still gave me the “the type or namespace ‘DisplayTools’ Could not be found.” error. I’m not sure what I’m doing wrong here…

You will also need to put one script type in standardasset
it is regarding compiling issue…
i forget what type to put in …
Try putting one type in the standard asset…

i think is putting javascript into standardasset

just put the script you wan to access in it

That doesn’t work either. It’s like Unity compiles the c# scripts first, no matter what folder they’re in…

Try this, create a Plugins folder, put the c# script in that forlder, do not put the javascript in that folder or in the Standards Assets or Pro Standard Assets folder (put javascript in one of your own folders). See if this solves anything, if you have not tried that

You can’t put them both in the same folder. You need to put the javascript file in the “Standard Assets” or “Plugins” folder, and the C# file in a different folder without a special name.

Chubbspet has the right idea but backwards; you need to put the javascript (the one you want to call) in the Plugins folder to make sure it gets compiled first, and the C# (the one doing the calling) in a different folder to make sure it gets compiled second.

the Standard Assets must named like this
I got this problem before…

so it sounds like what I need it to do can’t be done. basically I have 4 scripts that need to be compiled in this order:

Javascript_A
C#_A and C#_B
Javascript_B

but if I put the J_A script in the pugins folder with C#_A and C#_B, C#_B breaks. if I put J_A in StandardAssets and the C# scripts in Plugins, C#_B breaks. If I put J_A in StandardAssets or Plugins and C# scripts anywhere else, J_B breaks.

not sure what else to do besides re-think my plans…

You should look at that link chubbspet posted ( Unity - Scripting API: )

There are four steps you can use. So put JavaScript_A in “Standard Assets”, put C#_A and C#_B in “Standard Assets/Editor”, and then put Javascript_B in a normal folder.

yeah, I tried that. Javascript_B (which is in Asset/Scripts) breaks if I move the C# scripts out of StandardAssets or Plugins…

Ah, you’re right, things in a “normal” folder can only access things from group 1 (Standard Assets, Plugins, etc). You could put Javascript_A in “Standard Assets”, the C# files in “Standard Assets/Editor” and Javascript_B into “Editor”, and that should work, but then JavaScript_B can’t access any other scripts in normal folders, which is probably an issue.

As that page says, the easier thing might be to rewrite Javascript_B to use dynamic types instead of named types. So don’t put DisplayTools.Test(), do something like GetComponent(“DisplayTools”).Test()… you can’t use the class name “DisplayTools” in javascript_b but you can get it by its string name as a “mystery” type.

UnityScript is compiled after C#, I believe. If you’re using C# scripts for everything, you can just toss them all into the same folder and any of them would still be able to access each other.

I tried all sorts of variations on GetComponent, and none of them seemed to work.

I’m basically trying to get the game to act on items that were purchased using Tapjoy’s virtual goods (using Prime31’s plugin). Since all of this happens in the main menu level, I’m just reloading the level if the player purchases something…seems to work okay so far…