- Home /
How to call a c-sharp function from javascript?
Ok, the problem is i need to implement socket connections, but there i think there is no other way than implementing them on c sharp. But now i need to call the functions on my csharp file from javascript and it gives me a compile error, because it cannot find the function in chsarp file. It might be an issue related to compilation order. Can anyone suggest any solutions?
You shouldn't need C# to implement socket connections. See http://answers.unity3d.com/questions/58710/socket-connection-throught-javascript/58907#58907
Answer by Peter G · May 14, 2011 at 04:27 PM
Let's go through the quick check to make sure all of these statements are true:
Unity can find the class. If you get an error that your type doesn't exist, then you may have typed it in wrong or you might have a compilation order problem. SEE below.
You are accessing the function correctly:
instance.Method()
orClassName.StaticMethod()
You typed the method name in correctly (I know its obvious but its worth asking)
If none of these are true you might have an issue with compilation order.
See this page on what order scripts are compiled in. You really just need the C# script to be compiled before the js script. I would suggest either placing it in a "Plugins" folder or in "Standard Assets".
There is also a chance that you can compile them at the same level. There is no specific order that they will be compiled in, but I find often that C# scripts are compiled before js scripts on the same level. Note: That is by no means a guarantee that it will happen that way and you CANNOT rely on this random chance. I am merely stating what I observe more often than not
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Lower player's health from separate script. 2 Answers
Translate this statement from .js to C# 2 Answers
call a javascript function from c-sharp 2 Answers
Distribute terrain in zones 3 Answers