Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dlynch13 · Aug 04, 2012 at 07:19 PM · c#javascriptclass

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Aug 04, 2012 at 07:49 PM 0
Share

Out of curiosity, why are you bothering with this anyway? Why not just do it all with JS ins$$anonymous$$d of mixing languages?

avatar image dlynch13 · Aug 04, 2012 at 10:17 PM 0
Share

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.

avatar image Eric5h5 · Aug 04, 2012 at 10:37 PM 0
Share

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.

avatar image dlynch13 · Aug 04, 2012 at 11:45 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Aug 04, 2012 at 07:46 PM 0
Share

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.

avatar image Akill · Aug 04, 2012 at 07:47 PM 0
Share

good to know , thanks.

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dlynch13 · Aug 04, 2012 at 10:07 PM 0
Share

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.

avatar image Eric5h5 · Aug 04, 2012 at 10:35 PM 0
Share

> 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.

avatar image dlynch13 · Aug 04, 2012 at 10:38 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# into JavaScript 1 Answer

How to add javascript classes? 1 Answer

Construct class with enum parameter (javascript) 0 Answers

Is it possible to create a script dinamic like animation>size controling the Elements variables? 1 Answer

Reading and Storing External Data into Memory (From Text) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges