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;
right now it doesn't know what JSscript is.
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 SendMessage 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.
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?
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.
I tried moving things around but the error never changes. Is there another way besides sendmessage to call a script. Maybe something that wouldn't depend on the compilation order?
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 MonoDevelop will give you errors if you try to build from there, but everything works perfectly in Unity.
GameObject found = GameObject.Find ("Sphere");
var foundScript = found.GetComponent<testjs>() as testjs;
else Debug.LogError("No attached testjs script.");
else Debug.LogError("No Sphere");
Answer by kmeboe
Oct 10, 2012 at 07:26 PM
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
10 People are following this question.
Enum ToString via Int (JS)
How to do transform.LookAt() from a different part of object?
Fixing the networking example
remove quotation marks from a parsed string
How Do I Update a cSharp script var from js?