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 dansav · Oct 10, 2012 at 07:07 PM · javascriptcommunicationcs

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(){
 go=GameObject.Find("Sphere");
 myscript=go.GetComponent<JSscript>();
 }

right now it doesn't know what JSscript is.

Comment
Add comment · Show 7
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 dansav · Oct 10, 2012 at 07:35 PM 0
Share

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 Send$$anonymous$$essage 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.

avatar image kmeboe · Oct 10, 2012 at 07:51 PM 0
Share

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?

avatar image kmeboe · Oct 10, 2012 at 07:52 PM 0
Share

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.

avatar image dansav · Oct 10, 2012 at 08:05 PM 0
Share

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

avatar image kmeboe · Oct 10, 2012 at 08:28 PM 0
Share

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 $$anonymous$$onoDevelop will give you errors if you try to build from there, but everything works perfectly in Unity.

Here's my code. $$anonymous$$yRegularFunc 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)
         {
             foundScript.$$anonymous$$yRegularFunc();
         }
         else Debug.LogError("No attached testjs script.");
     }
     else Debug.LogError("No Sphere");
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kmeboe · Oct 10, 2012 at 07:26 PM

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

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

10 People are following this question.

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

Related Questions

How to use Android Dual Joysticks? 1 Answer

Space Craft controller. 1 Answer

call unity javascript from cs plugin 1 Answer

JavaScript 3 Arrays questions 1 Answer

JS file deleting multiple game objects 0 Answers


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