Search Unity

login system Scene not loading after button click buts php is good.

Discussion in 'Scripting' started by magstorey, Mar 20, 2019.

  1. magstorey

    magstorey

    Joined:
    Feb 20, 2019
    Posts:
    13
    Hi all, I am trying to create a login system for my final year at uni. I know the login works as i am gettin login successful in console log but the next scene wont load? i have the input fields attatched in unity i fear i am going round in circles and confusing myself.
    any help would be appreciated.
    Thanks


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Networking;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class logReg : MonoBehaviour
    9. {
    10.     public Text usernameInput, passwordInput;
    11.     public Text messageText; //THIS IS THE TEXT TO BE DISPLAYED ON SCREEN TO THE PLAYER
    12.     string username, password, message = "" ;
    13.  
    14.  
    15.     public void LogIn()
    16.     {
    17.         messageText.text = ""; //CLEAR ANY DISPLAYED MESSAGES TO THE PLAYER
    18.  
    19.         username = usernameInput.text;    //POPULATE THE PRIVATE username VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE usernameText INPUT FIELD
    20.         password = passwordInput.text;    //POPULATE THE PRIVATE password VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE passwordText INPUT FIELD
    21.  
    22.         if (username == "" || password == "") //IF THE PLAYER HASN'T ENTERED THE REQUIRED INFORMATION...TELL THEM TO
    23.             messageText.text = "Please complete all fields.";
    24.         else    //IF ALL INFORMATION IS ENTERED, BUILD A WWWForm AND SEND IT TO THE SERVER
    25.         {
    26.             WWWForm form = new WWWForm();
    27.             form.AddField("usernamePost", username);
    28.             form.AddField("passwordPost", password);
    29.             WWW w = new WWW("http://localhost:8080/Project/PHP/login.php", form);
    30.             StartCoroutine(LogIn(w));
    31.         }
    32.  
    33.     }
    34.     public IEnumerator LogIn(WWW _w)
    35.     {
    36.         yield return _w;    //WAIT FOR A RESPONSE FROM THE SERVER
    37.  
    38.         if (_w.error == null)    //IF THE SERVER DOESN'T SEND BACK AN ERROR
    39.         {
    40.             if (_w.text == "login successful")    //THE PHP SCRIPT SUPPLIED WILL SEND THIS MESSAGE BACK IF THE LOGIN WAS SUCCESSFUL
    41.             {
    42.              
    43.                 Debug.Log(_w.text);
    44.                 SceneManager.LoadScene("StartScreen");
    45.             }
    46.             else
    47.                 messageText.text = _w.text;    //THE PHP SCRIPT SUPPLIED WILL TELL THE PLAYER IF THEIR PASSWORD IS INCORRECT, OR IF THEIR USERNAME DOESN'T EXIST
    48.             Debug.Log(_w.text);
    49.         }
    50.         else
    51.             messageText.text = "ERROR: " + _w.error;    //IF THERE IS AN ERROR (SUCH AS THE SERVER BEING DOWN) THE PHP SCRIPT SUPPLIED WILL TELL THE PLAYER
    52.         Debug.Log(_w.text);
    53.     }
    54.  
    55. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    If your Debug.Log(_w.text); is showing, then your scene should be loading. Possible reasons it wouldn't is your name doesn't match or the scene wasn't added to your build settings. Usually the console should give an error for that.
     
  3. magstorey

    magstorey

    Joined:
    Feb 20, 2019
    Posts:
    13
    It's definitely the correct scene name and is in my build settings. When I click the submit button the script runs twice. Could that be the issue?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Do you have two copies of the script in your scene?
     
  5. magstorey

    magstorey

    Joined:
    Feb 20, 2019
    Posts:
    13
    just checked, doesnt seem so. I'm just so confused. all ive done since i posted this is change the file name to loginScript and now nothing works!
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Where is Login() called? Make sure you only call it once (in Visual Studio you can find all the call sites of the method, if they're in your source code).
    If you linked the method via Unity's onClick button events in the inspector, you should check that this entry exists only once.

    In general though, multiple calls should not prevent the scene from switching as long as one suceeds.

    Also, are you sure the response text is exactly the same as the one you check for? Perhaps you missed a cap letter or have a typo... You'd then see a messsage that's similar to the one you expect, and you might miss the fact that it's actually slightly different.

    I also recommend to simply attach the debugger and set a break point, step through your script... This helps to inspect values at any point and you'll have a better chance to follow the application flow.

    Last but not least, you said you changed the file name after posting and nothing seems to work at all anymore.
    Reason: For anything that's s concrete subtype of MonoBehaviour or ScriptableObject, the file name must match the type's name. Otherwise you won't be able to use the types as they're supposed to.
     
  7. magstorey

    magstorey

    Joined:
    Feb 20, 2019
    Posts:
    13
    ive gone back to the original file name but still not working, I updated the coroutine to just w rather than _w
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Networking;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class logReg : MonoBehaviour
    9. {
    10.     public Text usernameInput, passwordInput;
    11.     public Text messageText; //THIS IS THE TEXT TO BE DISPLAYED ON SCREEN TO THE PLAYER
    12.     private string username, password ;
    13.  
    14.  
    15.     public void LogIn()
    16.     {
    17.         messageText.text = ""; //CLEAR ANY DISPLAYED MESSAGES TO THE PLAYER
    18.  
    19.         username = usernameInput.text;    //POPULATE THE PRIVATE username VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE usernameText INPUT FIELD
    20.         password = passwordInput.text;    //POPULATE THE PRIVATE password VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE passwordText INPUT FIELD
    21.  
    22.         if (username == "" || password == "") //IF THE PLAYER HASN'T ENTERED THE REQUIRED INFORMATION...TELL THEM TO
    23.             messageText.text = "Please complete all fields.";
    24.         else    //IF ALL INFORMATION IS ENTERED, BUILD A WWWForm AND SEND IT TO THE SERVER
    25.         {
    26.             WWWForm form = new WWWForm();
    27.             form.AddField("usernamePost", username);
    28.             form.AddField("passwordPost", password);
    29.             WWW w = new WWW("http://localhost:8080/Project/PHP/login.php", form);
    30.             StartCoroutine(LogInn(w));
    31.         }
    32.  
    33.     }
    34.     public IEnumerator LogInn(WWW w)
    35.     {
    36.         yield return w;    //WAIT FOR A RESPONSE FROM THE SERVER
    37.  
    38.         if (w.error == null)    //IF THE SERVER DOESN'T SEND BACK AN ERROR
    39.         {
    40.             if (w.text == "login successful")    //THE PHP SCRIPT SUPPLIED WILL SEND THIS MESSAGE BACK IF THE LOGIN WAS SUCCESSFUL
    41.             {
    42.              
    43.                 Debug.Log(w.text);
    44.                 SceneManager.LoadScene("StartScreen");
    45.             }
    46.             else
    47.                 messageText.text = w.text;    //THE PHP SCRIPT SUPPLIED WILL TELL THE PLAYER IF THEIR PASSWORD IS INCORRECT, OR IF THEIR USERNAME DOESN'T EXIST
    48.             Debug.Log(w.text);
    49.         }
    50.         else
    51.  
    52.         Debug.Log(w.text);
    53.     }
    54.    
    55. }
     
  8. magstorey

    magstorey

    Joined:
    Feb 20, 2019
    Posts:
    13
    ok so i got it working again, (forgot to call Login) but still not loading next scene, this is the result of the console log...
    login successful
    UnityEngine.Debug:Log(Object)
    <LogIn>c__Iterator0:MoveNext() (at Assets/scripts/logReg.cs:50)
    UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
     
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Seems like you're not even getting into the branch that loads the scene.

    Try checking the error with string.IsNullOrEmpty. The documentation of the error property states, that you should check against 'null' and empty strings. Depending on the target platform, either of these may indicate that there's no errror.

    If that still doesn't work, I can only recommend to step through your code with the debugger.

    Btw, WWW has been deprecated in recent versions. Check out UnityWebRequests.