Hi, I have two scripts. Userdata is used to access my database(phpmyadmin and xampp), I import the data from a website, now my code is woolly as I haven’t been able to test individual parts. in the script userdata, I use a while loop to search through all of the records in the table and then when a result is found it attempts to store the relevant data from my 2d array of database results into 3 variables and break out of the while loop. It throws an error when storing the 2d array items into string variables. Then afterwards I want to use the results in another script however I don’t know how to do this. Also yes I know I am using the old GUI stuff and should switch to the newer version ![]()
This is the script where I want to access the result variables.
public class ILogin : MonoBehaviour
{
public IStudent IStudent;
public GameObject StudentScreen;
public string UsernameInput = string.Empty;
public string PasswordInput = string.Empty;
public Boolean Student = false;
public int row;
private Rect windowRect = new Rect(0, 0, Screen.width, Screen.height);
private string AccountType;
//private object userArray;
public void OnGUI()
{
GUI.Window(0, windowRect, WindowFunction, "Login");
}
public void WindowFunction(int windowID)
{
//user input log in
UsernameInput = GUI.TextField(new Rect(Screen.width / 3, 2 * Screen.height / 5, Screen.width / 3, Screen.height / 10), UsernameInput, 10);
PasswordInput = GUI.PasswordField(new Rect(Screen.width / 3, 2 * Screen.height / 3, Screen.width / 3, Screen.height / 10), PasswordInput, "X"[0], 10);
GUI.Label(new Rect(Screen.width / 3, 35 * Screen.height / 100, Screen.width / 5, Screen.height / 8), "Username");
GUI.Label(new Rect(Screen.width / 3, 62 * Screen.height / 100, Screen.width / 8, Screen.height / 8), "Password");
if (GUI.Button(new Rect(Screen.width / 3, 4 * Screen.height / 5, Screen.width / 8, Screen.height / 8), "Log-in"))
{
GetComponent<Userdata>().CallDetail(UsernameInput, PasswordInput);
if (DBUsername == UsernameInput && DBPassword == PasswordInput)
{
//Check account type
if (AccountType == "STAFF")
{
//call Staff interface
Debug.Log("STAFF");
SceneManager.LoadScene("Staff");
}
else
{
//Call Student Interface
Debug.Log("STUDENT");
SceneManager.LoadScene("Student");
}
}
else
{
GUI.Label(new Rect(Screen.width / 2, Screen.height / 2, Screen.width / 12, Screen.height / 12), "Incorrect Login Details");
}
}
}
}
Then the Userdata Script that accesses the table.
public class Userdata : MonoBehaviour
{
string url = "http://localhost/Wellbeing/userdata.php";
private string Temp;
public string[] users;
public string[] userArray;
private int row = 0;
private bool Found = false;
public string DBUsername;
public string DBPassword;
public string AccountType;
public IEnumerator CallDetail(string UsernameInput, string PasswordInput)
{
Debug.Log("hi");
WWW userData = new WWW(url);
yield return userData;
string userDataString = userData.text;
users = userDataString.Split(';');
Debug.Log(users);
for (var i = 0; i < users.Length; i++)
{
Temp = users[i];
userArray = Temp.Split('|');
Debug.Log(userArray);
}
while (Found != true)
{
if (userArray[row] == UsernameInput && userArray[row] == PasswordInput)
{
DBUsername = userArray[row][2];
DBPassword = userArray[row][1];
AccountType = userArray[row][6];
Found = true;
}
else
{
row++;
}
}
yield return DBUsername;
yield return DBPassword;
yield return AccountType;
}
}
Many Thanks