Issue with loging class and www form

Hi,
one question - why this is not working?

The error says:

Line 152 is userData.login();
This is the first script with GUI that calls another object (UserData)
Even though error is saying that DrawLogin has a bug, the problem should be in the UserData class.
Because it seams to work if I comment out WWW initialization.

var uEmail = "";
		var uPass = "";
		
		function DrawLogin (windowID : int){
			GUILayout.Label("Enter User Name and Password");
			//uName = GUILayout.TextField (uName, 100);
			uEmail = GUILayout.TextField (uEmail , 100);
			//uPass = GUILayout.PasswordField (uPass, "[ ]"[0], 100);
			uPass = GUILayout.PasswordField (uPass , "#"[0], 100);
			
			if (GUI.Button (Rect (90,100,100,20), "Login")){
				var userData : UserData = new UserData();		
				userData.email = uEmail;
				userData.pass = uPass;
				userData.login();
			}
			
		}

UserData Class:

class UserData extends MonoBehaviour{

	static var email : String;
	static var pass : String;

	
var URL = "http://somewww/usercheck.php";

	function login(){
		print (pass);
		print (email);
		var form = new WWWForm(); 
		form.AddField( "email", email );
		form.AddField( "pass", pass );

		var www : WWW = new WWW(URL, form); //ANY PROBLEMS HERE?
		yield www; 
		
		if (www.text == "Success") {
			print("Success");
			Application.LoadLevel (2);
		} else {
			print("Error");
		}
	}

Initially, I would have said userData was null, but looking at your code, I don’t see how that could be the case.

I’m going to take a shot in the dark here and say that this might be fired from an Update function? If so, I think you need to start that process as a coroutine. I might not be the best source on this though: I avoided yields like the plague with my application development.

The problem was that for some reason i can’t use var userData : UserData = new UserData();
Instead i used this one and it works:

var userData : UserData;
**userData = GetComponent (UserData); **
userData.login();

Could some one please tell me why the first way is wrong? It’s how i would do it in PHP or Java…

because MonoBehaviors can’t be created through new, only through adding them to gameobjects

If you want to “new” it and not attach it to objects, derive it from Object through

class MyUnityScriptClass extends Object

if no class declaration is present, UnityScript classes always extend MonoBehaviour (just for clarification sake)

Woops, I knew I missed something. (though I would have expected a different error in that case, something about instantiating monobehaviours or something; I’m sure I’ve seen it before) Good catch, dremora.

ok. i see now.
Thanks guys!