unity3d - I keep getting "IndexOutOfRangeException" on my Unity project. UNITY + PHP
Get the solution ↓↓↓(I was trying to follow this tutorial https://www.youtube.com/watch?v=NVdjlXgbiMM&t=6s)
I'm trying to implement a login feature in a Unity project, I've successfully done the register part and it works, but I can't get the login part to work.
Every time I try to get info from the database, it gives me the "IndexOutOfRangeException" on C#. I think that the problem is in the PHP part tho, since the C# seems to be fine.
C# Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Text.RegularExpressions;
public class login : MonoBehaviour
{
public InputField namefield, passwordfield;
public Button submitButton;
public GameObject popUp;
public Text text;
public void CallLogin()
{
StartCoroutine(Login2());
}
IEnumerator Login()
{
WWWForm form = new WWWForm();
form.AddField("name", namefield.text);
form.AddField("password", passwordfield.text);
WWW www = new WWW("http://localhost/sqlconnect/login2.php", form);
yield return www;
Debug.Log(www.text);
if (www.text[0] == '0')
{
bddManeger.username = namefield.text;
bddManeger.tempo = int.Parse(www.text.Split('\t')[1]);
}
else
{
Debug.Log("User Login Failed. Error #" + www.text);
}
}
IEnumerator Login2()
{
WWWForm form = new WWWForm();
form.AddField("name", namefield.text);
form.AddField("password", passwordfield.text);
UnityWebRequest www = UnityWebRequest.Post("http://localhost/sqlconnect/login2.php", form);
yield return www.SendWebRequest();
Debug.Log(www.downloadHandler.text);
if (www.downloadHandler.text[0] == '0')
{
bddManeger.username = namefield.text;
bddManeger.tempo = int.Parse(www.downloadHandler.text.Split('\t')[1]);
UnityEngine.SceneManagement.SceneManager.LoadScene(2);
}
else
{
Debug.Log("User Error: " + www.downloadHandler.text);
}
}
private void Update()
{
VerifyInputs();
}
public void VerifyInputs()
{
submitButton.interactable = (namefield.text.Length >= 8 && passwordfield.text.Length >= 8);
if (Input.GetKeyDown(KeyCode.Tab))
{
if (namefield.GetComponent<InputField>().isFocused)
{
passwordfield.GetComponent<InputField>().Select();
}
if (passwordfield.GetComponent<InputField>().isFocused)
{
namefield.GetComponent<InputField>().Select();
}
}
}
}
PHP
<?php
$con = mysqli_connect('localhost', 'root', 'root', 'apolobdd');
//check connection
if(mysqli_connect_errno())
{
echo "1: falha na conecção";
exit();
}
$username = $_POST["name"];
$password = $_POST["password"];
//check name
$namecheckquery ="SELECT username, salt, hash, tempo FROM users WHERE username = '" . $usename . "';";
$namecheck = mysqli_query($con, $namecheckquery) or die ("2: Verificação Falhou");
if (mysqli_num_rows($namecheck) != 0)
{
echo "5: either no user with name or more than one";
exit();
}
//get login info from query
$existinginfo = mysqli_fetch_assot($namecheck);
$salt = $existinginfo["salt"];
$hash = $existinginfo["salt"];
$loginhash = crypt($password, $salt);
if ($hash != $loginhash)
{
echo "6: Incorrect Password";
exit();
}
echo "0\t" . $existinginfo["tempo"];
?>
Answer
Solution:
Your problem is UnityWebRequest.text is a string and you are treating it like an array. Code some script to parse the string as needed.
https://docs.unity3d.com/ScriptReference/Networking.DownloadHandler-text.html
if (www.downloadHandler.text[0] == '0')
and possibly there may be an issue here as www is deprecated
if (www.text[0] == '0')
https://docs.unity3d.com/ScriptReference/WWW.html
Try WWWForm instead
Answer
Solution:
just here to update.
I didn't fully understand the code and what it did, and there were some mistakes on my part of the code.
But thanks to the people who tried to help anyways.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreach() argument must be of type array|object, null given
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.