- Home /
Call CSharp class from JS Error- Dumbfounded
I know all about script execution order. I have my CSharp script in the Standard Assets folder and my JS script in Standard Assets/Scripts/Utility.
Here is the CSharp file WaveGenerator.cs in Standard Assets:
using UnityEngine;
using System.Collections;
using System;
public class WaveGenerator {
Queue myQ;
public WaveGenerator() {
myQ = new Queue();
}
}
Here is the call from JS (in the Start() function, in Standard Assets/Scripts/Utility):
var waveGenerator = new WaveGenerator();
I get "Unknown Identifier" when I try to compile. Why? Thanks.
Out of curiosity, why are you bothering with this anyway? Why not just do it all with JS ins$$anonymous$$d of mixing languages?
I started my project in JS, but then found out it does not have Queue or Struct functions. Those are possible, but not straightforward and I could not get them working in JS. What a load of crap. So now I have to re-write the whole thing in C# or Boo, or learn how to call functions from more powerful languages. For now I am going to call a more powerful function from another language. Besides, someday I may want to use a plug-in from some other language and I need to know how to do it.
That's not correct; Queue is used in JS exactly like it is in C#. It's completely 100% straightforward; it's a function of .NET/$$anonymous$$ono and not related to C# specifically anyway.
var q = new Queue();
q.Enqueue(1);
q.Enqueue(2);
for (var val in q) Debug.Log (val);
Although you should probably be using the generic version.
Structs are slightly less straightforward, but all you have to do is:
class Foo extends System.ValueType {...
and then Foo is a struct. Aside from having to write "extends System.ValueType" ins$$anonymous$$d of "struct", it works exactly the same.
Excellent. I tried this again following your example and got it working. You are right. Thanks for convincing me. Sometimes we wander off in the dark all alone and need a voice of reassurance to find the way again.
Answer by dlynch13 · Aug 04, 2012 at 10:49 PM
I figured it out. The documentation was correct, but you have to read it super carefully. It could sure use a simple example, like most things in the reference docs. You cannot put the JS code in a subfolder of Standard Assets. It has to be in a different path completely. I created a "Scripts" folder under my root Project in the Isnpector and moved all of my JS files to there. Then, the CSharp file that is in Standard Assets was correctly compiled before my JS files. Whew.
Answer by Akill · Aug 04, 2012 at 07:23 PM
Never used javascript before, but when creating custom objects in every language I have used, you need to specify the type of variable, before you try and create it.
i.e. var waveGenerator : WaveGenerator;
All languages in Unity use type inference (including C#), where the type of the variable is inferred by the value supplied. "var waveGenerator = new WaveGenerator();" is valid C# and Unityscript code.
Answer by Eric5h5 · Aug 04, 2012 at 07:48 PM
Your js script must not be in Standard Assets. Might want to read up on script compilation order.
I have been through that link many times. It says this in step 1: All scripts in "Standard Assets", "Pro Standard Assets" or "Plugins" are compiled first.
Then, in step 3 it says this: if you want to create a Javascript that uses a C# script: place the C# script in the "Standard Assets" folder and the Javascript outside of the "Standard Assets" folder. The Javascript can now reference the C# script directly.
I am doing exactly what that link says to do, and I still get an error that the WaveGenerator is an $$anonymous$$ Identifier. This is why I am stumped.
> place the C# script in the "Standard Assets" folder and the Javascript outside of the "Standard Assets" folder
This is what you are not doing...your JS script is still in Standard Assets. You have to do what the docs say and place the JS script outside Standard Assets.
Right, I just figured that out before you posted. "outside" is the operative word. I thought that Standard Assets/Utility was outside of Standard Assets. I didn't realize that it meant at a higher level in the structure. Some examples in the docs would go a long way.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075202im_/https://answers.unity.com/themes/thub/images/avi.jpg)