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 ina · Jan 21, 2012 at 04:42 PM · c#getcomponentreferencetype

Referencing a c# component script from JS script

How do you reference a c# component script from a JS script? Do you need to store the reference as you would with a JS component, to prevent having to fetch GetComponent each time?


Trying the usual below for a c# script named CSharpComp.cs, I get this error: 'CSharpComp' does not denote a valid type ('not found').

myfile.js contains:

var csharpcomponent:CSharpComp;

function Start(){

csharpcomponent = GameObject.Find("MainObj").GetComponent(CSharpComp);

}

Comment
Add comment
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

2 Replies

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

Answer by aldonaletto · Jan 21, 2012 at 04:56 PM

C# and JS can't see each other at compile time (different compilers are used for each language), but already compiled scripts can be seen by any language. If you place the C# script in Plugins or StandardAssets, and the JS script in another Assets folder (Assets/Scripts, for instance), the C# script will be compiled in the "first wave", thus the JS script will know it and compile ok (see Script Compilation). NOTE: this is a one way via: the CS script won't be able to see the scripts not yet compiled (JS or C#).
If you just need to call some CSharpComp function, SendMessage("FunctionName", argument) can be used. This is a very limited alternative, since SendMessage accepts only a single parameter, but it works independently of language or compilation order (it's a runtime feature).

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 ina · Jan 21, 2012 at 04:59 PM 0
Share

Also, I read Send$$anonymous$$essage should be used sparingly since it is very slow and impacts performance

avatar image ina · Jan 21, 2012 at 05:14 PM 0
Share

It looks like this does not work exactly, as $$anonymous$$ainObj contains the CSharpComp.cs that myfile.js is trying to refernece... the GetComponent does not work!

avatar image aldonaletto · Jan 22, 2012 at 02:27 AM 0
Share

It's weird! I tested a JS script in Assets/Scripts and it found $$anonymous$$ouseLook.cs without any problem ($$anonymous$$ouseLook.cs is stored in Standard Assets/Character Controllers/Sources/Scripts).
And you're right about Send$$anonymous$$essage: it must check all scripts and look for the specific function in each one, thus using Send$$anonymous$$essage frequently isn't a good idea.

avatar image
0

Answer by Kryptos · Jan 21, 2012 at 07:32 PM

You can set the assembly dependencies in the MonoDevelop project. Right click on the 'References' folder under the Assembly-UnityScript project and 'Edit Preferences'.

Again, cyclic references are not allowed so you can't make both projects see each other.

Anyway, this is bad practice to have a project with both JS and C#. You should consider translating all scripts to one unique language.

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

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

6 People are following this question.

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

Related Questions

Error when referencing one script from another. 1 Answer

.getComponent() creates an Instance? 1 Answer

Referencing booleans from settings 1 Answer

Get Rigidbody2D component in script (c#) 1 Answer

Is it possible to use a variable as a Type? 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