title: Unity Login Button
body: <p><strong>Update Fix</strong></p>
  
  <p>I managed to fix my problem by reformatting my code and using the correct IEmu function here is the fixed code</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public class Click : MonoBehaviour 
  {
      public static string user = "", name = "";
      private string password = "", rePass = "", message = "";
      public UnityEngine.UI.Text userString;
      public UnityEngine.UI.Text passString;
      private bool register = false;
  
  
      public int CurrentQuestion;
      int levelChanger;
  
      void Start(){
          levelChanger = 1;
          print (CurrentQuestion);
      }
  
      public void Clicked()
      {
          if (message != "")
              print (message);
  
  
              PlayerPrefs.SetInt ("CurrentQ",levelChanger);
              message = "";
  
              if (userString.text == "" || passString.text == "")
                  message += "Please enter all the fields \n";
              else
              {
                  WWWForm form = new WWWForm();
                  form.AddField("user", userString.text);
  
                  form.AddField("password", passString.text);
                  WWW w = new WWW("localhost", form);
                  StartCoroutine(login(w));
          }
  
  
  
      }
  
  
  IEnumerator login(WWW w)
  {
      yield return w;
      if (w.error == null)
      {
          if (w.text == "login-SUCCESS")
          {
              PlayerPrefs.SetString("Username",userString.text);
              Application.LoadLevel(1);
              print("WOOOOOOOOOOOOOOO!");
          }
          else
              message += w.text;
      }
      else
      {
          message += "ERROR: " + w.error + "\n";
      }
  }
  
  }
  </code></pre>
  
  <p>I have this problem which keeps coming up whenever i try and get my login working it works with a GUILayout but when i try change it to a canvased system i am getting some errors. 
  these errors are </p>
  
  <pre><code>InvalidOperationException: operation is not valid due to the current state of object.... 
  </code></pre>
  
  <p>Here is my code </p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public class database : MonoBehaviour 
  {
      public static string user = "", name = "";
      private string password = "", rePass = "", message = "";
      public UnityEngine.UI.Text userString;
      public UnityEngine.UI.Text passString;
      public UnityEngine.UI.Button LoginClick;
      private bool register = false;
  
      private void OnGUI()
      {
          if (message != "")
              GUILayout.Box(message);
  
          if (register)
          {
              GUILayout.Label("User");
              user = GUILayout.TextField(user);
              GUILayout.Label("Name");
              name = GUILayout.TextField(name);
              GUILayout.Label("password");
              password = GUILayout.PasswordField(password, "*"[0]);
              GUILayout.Label("Re-password");
              rePass = GUILayout.PasswordField(rePass, "*"[0]);
  
              GUILayout.BeginHorizontal();
  
              /*if (GUILayout.Button("Back"))
                  register = false;
  
              if (GUILayout.Button("Register"))
              {
                  message = "";
  
                  if (user == "" || name == "" || password == "")
                      message += "Please enter all the fields \n";
                  else
                  {
                      if (password == rePass)
                      {
                          WWWForm form = new WWWForm();
                          form.AddField("user", user);
                          form.AddField("name", name);
                          form.AddField("password", password);
                          WWW w = new WWW("http://f6-preview.awardspace.com/unitytutorial.com/register.php", form);
                          StartCoroutine(registerFunc(w));
                      }
                      else
                          message += "Your Password does not match \n";
                  }
              }*/
  
              //GUILayout.EndHorizontal();
          }
          else
          {
  
              user = userString.ToString ();
              password = passString.ToString ();
  
              if (LoginClick)
              {
                  message = "";
                  PlayerPrefs.SetString ("MyUser",user);
  
                  if (user == "" || password == "")
                      message += "Please enter all the fields \n";
                  else
                  {
                      WWWForm form = new WWWForm();
                      form.AddField("user", user);
                      form.AddField("password", password);
                      WWW w = new WWW("DuntbeSilly:3", form);
                      StartCoroutine(login(w));
                  }
              }
  
              //if (GUILayout.Button("Register"))
              //  register = false;
  
              GUILayout.EndHorizontal();
              GUILayout.EndArea();
          }
      }
  
      IEnumerator login(WWW w)
      {
          yield return w;
          if (w.error == null)
          {
              if (w.text == "login-SUCCESS")
              {
                  PlayerPrefs.SetInt ("CurrentQ",Score.CurrentQuestion +=1);
                  Application.LoadLevel(PlayerPrefs.GetInt("CurrentQ"));
                  print ("changelevel");
              }
              else
                  message += w.text;
          }
          else
          {
              message += "ERROR: " + w.error + "\n";
          }
      }
  
      IEnumerator registerFunc(WWW w)
      {
          yield return w;
          if (w.error == null)
          {
              message += w.text;
          }
          else
          {
              message += "ERROR: " + w.error + "\n";
          }
  
      }
  }
  </code></pre>