- Home /
UnityWebRequest Post Not Returning Anything
The player gets registered in the database succesfully but it seems like the code after 'yield return www.SendWebRequest()' is never executed. Not a single Debug.Log() is showing up in my console.
Can someone please help me out?
C# code:
private IEnumerator Register()
{
WWWForm form = new WWWForm();
form.AddField("name", "Anonymous");
form.AddField("highscore", "0");
Debug.Log("Sending now!");
UnityWebRequest www = UnityWebRequest.Post(API_URL + REGISTER_PAGE, form);
yield return www.SendWebRequest();
// These debug.logs don't show in console, but the player is added to the database without any issues.
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError("There was an error: " + www.error);
}
else
{
Debug.Log("Registered succesfully.");
string name = www.downloadHandler.text;
PlayerPrefs.SetString("name", name);
}
}
PHP code:
include("../connection.php");
$name = $_POST["name"];
$highscore = $_POST["highscore"];
$stmt = $connection->prepare("SELECT MAX(ID) FROM Leaderboard");
$stmt->execute();
$last_id = $stmt->fetchColumn();
if($name === "Anonymous")
{
if($last_id == false)
{
$last_id = 0;
}
else
{
$last_id = ((int)$last_id);
$last_id++;
}
$name = ($name."[".$last_id."]");
}
$stmt = $connection->prepare("INSERT INTO Leaderboard (name, highscore) VALUES (:name, :highscore)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':highscore', $highscore);
$stmt->execute();
echo $name;
$connection = null;
PS. Already tested the API in Postman (API-testing software) and it's working.
Check your console buttons. On top right corner there are 3 buttons, maybe you have logs disabled.
Was one of the first things I checked. The "Sending now!" get's printed though.
Answer by xxmariofer · Oct 24, 2019 at 01:52 PM
IEnumerator doesnt continue after the yield return if your object is disable/destroyed, are you maybe disabeling it? can you try using this code and check if the issue it is that is not done, or simply not coninue after the request? (just remove the yield from the request and add the while)
www.SendWebRequest();
while(!www.isDone)
{
Debug.Log("is waiting");
yield return null;
}
Debug.Log("done!");
@xxmariofer, you're a legend. The IEnumerator was indeed attached to a GameObject which state was changing. I put it on a seperate GameObject and now it's working. Thank you so much.
You are welcome, mark it as correct answer for future readers :)