Loading Scene with conditions

hi so i am using a .php file to communicate with a sql server for my game, i am trying to write a script for my log in screen to go to a network lobby buy i am having some trouble. i have debuged the user and password checking and it works fine but the scene loads no matter what. i would like it to only load if the user successfully logs in. any help would be appreciated

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Login : MonoBehaviour
{
    public InputField _userInputUserName;
    public InputField _userInputPassword;

    private string inputUserName;
    private string inputPassword;

    //public void ChangeToScene (int changeToScene);

    string LoginURL = "http://localhost/MeowMeowMeow/Login.php";

    // Use this for initialization
    void Start ()
    {
      
    }


  
    // Update is called once per frame
    void Update ()
    {
        //if (Input.GetKeyDown(KeyCode.L))
        ///{
        //    StartCoroutine( LoginToDB (inputUserName, inputPassword));
        //}
    }

    public void OnLogin()
    {
        inputUserName = _userInputUserName.text;
        inputPassword = _userInputPassword.text;
        StartCoroutine (LoginToDB (inputUserName, inputPassword));
        GoToLobby ();


    }

    void GoToLobby()
    {
        if(_userInputUserName.text == inputUserName && _userInputPassword.text == inputPassword)
        {
            SceneManager.LoadScene ("Lobby");
        }
          
    }

    IEnumerator LoginToDB(string username, string password)
    {
        WWWForm form = new WWWForm();
        form.AddField("usernamePost", username);
        form.AddField ("passwordPost", password);

        WWW www = new WWW (LoginURL, form);

        //waits for result then returns
        yield return www;

        Debug.Log (www.text);
    }
}

This is at least the second time you’ve posted this, right?

I think I tried to explain this to you before: You’re copy the ‘.text’ to a variable (and the password) and then sending that to a method where you check if they’re the same – of course they’re the same, b/c you just copied them.

However, in addition to that, you’re calling a coroutine then the previously mentioned method right after it, before the coroutine necessarily finishes (probably before, eh).

I can only imagine that you want to send the text entered to the server, check that they are correct, and then join the lobby if they are, right?

yea its the second time, i couldn’t figure out how to find things i posed before so i posed it again.

i was just wondering if it could be done in just unity of if i would have to modify my sever script as well

thanks for the slight insight to where i was going wrong, and the condescending additude

Go to your profile:
https://forum.unity3d.com/members/pianomeow.945283/

And click on ‘find threads by PianoMeow’:
https://forum.unity3d.com/search/7412527/

The resulting list is default sorted by age, the second post now (as of this moment) is your original thread:

thanks, i wanted to see the original b/c i knew it had an answer, just couldn’t find it

Well, my post wasn’t meant to be condescending… I was just remembering a script like this before, is all.
And I tried to point out what was happening in the script - things you might consider altering to achieve your desired result (such as what went first, and using the server feedback before proceeding)…

For example: get the result from the server, then go to the lobby if it’s correct.

here is my solution for anyone having the same questions

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Login : MonoBehaviour
{
    public InputField _userInputUserName;
    public InputField _userInputPassword;

    private string inputUserName;
    private string inputPassword;

    //public void ChangeToScene (int changeToScene);

    string LoginURL = "http://localhost/MeowMeowMeow/Login.php";

    // Use this for initialization
    void Start ()
    {
       
    }


   
    // Update is called once per frame
    void Update ()
    {
        //if (Input.GetKeyDown(KeyCode.L))
        ///{
        //    StartCoroutine( LoginToDB (inputUserName, inputPassword));
        //}
    }



    public void OnLogin()
    {
        inputUserName = _userInputUserName.text;
        inputPassword = _userInputPassword.text;
        StartCoroutine (LoginToDB (inputUserName, inputPassword));


    }




    IEnumerator LoginToDB(string username, string password)
    {

            WWWForm form = new WWWForm();
            form.AddField("usernamePost", username);
            form.AddField ("passwordPost", password);


            WWW www = new WWW (LoginURL, form);

            //waits for result then returns
            yield return www;
            if(www.text == "login success")
            {
                SceneManager.LoadScene ("Lobby");
            }
        Debug.Log (www.text);

    }
}

and my .php file for reference

<?php
{
    $servername = "localhost";
    $server_username = "root";
    $server_password = "";
    $dbName = "MeowMeowMeow";
   
    $username = $_POST["usernamePost"];
    $password = $_POST["passwordPost"];
   
   
    //Make Connection
    $conn = new mysqli($servername, $server_username, $server_password, $dbName);
   
    //Check Connecion
    if(!$conn)
    {
        die("Connection Failed.".mysqli_connect_error());
    }
   
    $sql = "SELECT password FROM users WHERE username = '".$username."'";
    $result = mysqli_query($conn ,$sql);
   
    //get result and confirm login
    if(mysqli_num_rows($result)>0)
    {
        //show data for eatch row
        while ($row = mysqli_fetch_assoc($result))
        {
            if($row['password'] === $password)
            {
                echo "login success";
            }
            else
            {
                echo "password incorrect";
            }
        }
       
    }
    else
    {
        echo "user not found";
    }
   
}
   
?>

Looks good, nicely done :slight_smile: