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");
    }
}

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.