- Home /
Calling JS Static Function from C# Script
I'm sorry I'm asking a question about something that has been asked a thousand times, I've read a couple other answers but none of them helped me. I have a script called php.js which has a publiic static function inside it called login. From my C# script I am trying to call this function, both scripts are attached to the same gameobject.
I've already put the php.js on first pass compile by putting it inside plugins. On VS it does recognize php when I use it on php PHPSCRIPT = GetComponent(); And then on the function that should be calling the login() I have PHPSCRIPT.login();
No compile errors or warnings, it is just not working, I added a debug.log("Called") inside the login() to check if it is getting called but it is not showing up Called so it is not getting called. What am I doing wrong?
php.js #pragma strict
public var userID : int;
public var hasLogged : boolean = false;
public function PHPLOGIN (user : String, password : String)
{
Debug.Log("Called2");
var url = "http://www.****.com/unity.php?username="+user+"&password="+password;
var w = WWW(url);
print(w.url);
yield w;
if (w.error != null)
{
print(w.error);
userID = 0;
}
else
{
var formText = w.text;
print(w.text);
userID = int.Parse(formText);
w.Dispose();
if (userID > 0)
{
hasLogged = true;
Debug.Log("Logged in with ID: " + userID);
}
}
}
networkingScript.cs
public class networkingScript : MonoBehaviour
{
public php PS;
void Start()
{
PS = GetComponent<php>();
}
public void logIn(string user, string password)
{
PS.PHPLOGIN(user, password);
Debug.Log("Called");
}
}
Answer by Bunny83 · Oct 26, 2014 at 08:10 PM
Your PHPLOGIN function is a coroutine. You have to use StartCoroutine in C#. UnityScript aitomagically does this for you when you "call" a coroutine. However i would suggest to wrap the coroutine thing in the UnityScript class.
Your naming makes it really difficult to read the code. Some general coding rules:
Class and method names always start with a capital letter. You usually use the "UpperCamelCase".
Identifiers that are ALLCAPS are usually constants. Normal variable names should use the "lowerCamelCase".
Use class names that describe what the class is good for. Don't use too long class names, but at least one that's more descriptive.
So to solve your problem:
// php.js
public var userID : int;
public var hasLogged : boolean = false;
private function LoginCoroutine (user : String, password : String)
{
var url = "...";
var w = WWW(url);
yield w;
// [...]
}
public function Login(user : String, password : String)
{
LoginCoroutine(user, password);
}
In your C# script:
public void LogIn(string user, string password)
{
PS.Login(user, password);
}
The shortest fix (if you want to ignore my naming advice) is to use StartCoroutine. So in your original C# script just do
StartCoroutine(PS.PHPLOGIN(user, password));
instead of
PS.PHPLOGIN(user, password);
That should do it.
Ohh, i just read your title again... The method in your UnityScript file is not "statis". I'm not sure why you wrote that. Do you know what static means?
Also as last advice: Try to avoid mixing languages! Otherwise you'll get into trouble in the long run. It might work in some small / simple applications or games but for bigger projects, pick one language.
Thanks alot for the great detailed answer :) About the static thing, at first I had at as static which made me think that could be the problem, so I made it non-static (instanced) but it changed nothing that's why I left it like that by accident hah. Also about the na$$anonymous$$g, yes you're right, that part of my code has messed up na$$anonymous$$g, I usually do it like that at first to build the code quick, then I make it work and pulish it and then figure out correct names :P.
And last, yeah I hate mixing languages too, I really don't want to handle JS but since I know nothing about php and about connecting to a webserver using unity I was forced to use JS since I couldn't find a tutorial on it which used C# and I don't know anything about co-routines neither so, yeah I was forced to :/.
And again thank you very much :) I did what you said about making a auxiliary function which started the coroutine function on the JS script and it worked perfectly :D. I'm just waiting until I know enough php and co-routines in C# to work out a C# php script by my-self.
Your answer
Follow this Question
Related Questions
How to call a c-sharp function from javascript? 1 Answer
Question about classes [will specify once I know what the name of what I'm looking for is] 0 Answers
Sending a message from javascript to C# 1 Answer
get component javascript from a c# script 3 Answers
What is this C# code in javascript 0 Answers