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?
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.