Problem with coroutine / yield
Hello,
Im using a piece of SQL within PHP to populate a popuplist.
PHP is really simple, just
<?php
$con = mysqli_connect('localhost','root','xxxxx','xxxxx') or ("#Cannot connect!" . mysqli_error());
if (!$con)
echo('#Could not connect: ' . mysqli_error());
$result = mysqli_query($con,"SELECT * from location");
while ($row = mysqli_fetch_array($result))
echo $row['LocationID'] . ":"
. $row['City'] . ":"
. $row['Description'] . "\n";
mysqli_close($con);
?>
Wamp server is running and when I open the php in a browser it echoes as expected.
Im trying to use the popuplist in 2 different scenes. It works in one scene... but in the other it seems that the code 'halts' at the yield command.
The scriptcode is;
using UnityEngine;
using System.Collections;
public class GetAllLocations : MonoBehaviour
{
public void Start() {
WWWForm form = new WWWForm ();
form.AddField ("name", "");
WWW w = new WWW ("http://localhost/EzonePlayer/getalllocations.php", form);
StartCoroutine (getlocFunc (w));
}
IEnumerator getlocFunc(WWW w)
{
Debug.Log("Marker0");
yield return w;
Debug.Log("Marker1");
if (w.error == null)
{
Debug.Log("Marker2");
//Removed working code here to populate popuplist, not relevant for problem
}
else
{
Debug.Log("Marker3");
//Removed working code here, not relevant for problem
}
}
}
In the working scene debug Marker0, 1 and 2 will show. In the not working scene only Marker0 shows.
I really can't figure out why the code works in one scene and doesnt work in the other scene. Anybody has any ideas what could be the problem?
I already tried renaming the coroutine to another name than in the other scene,
Thanks a lot in advance =)
Answer by Kyle_WG · Mar 16, 2017 at 02:27 PM
Try putting the WWW object as a member variable.
using UnityEngine;
using System.Collections;
public class GetAllLocations : MonoBehaviour
{
WWW w = null;
public void Start() {
WWWForm form = new WWWForm ();
form.AddField ("name", "");
w = new WWW ("http://localhost/EzonePlayer/getalllocations.php", form);
StartCoroutine (getlocFunc ());
}
IEnumerator getlocFunc()
{
Debug.Log("Marker0");
yield return w;
Debug.Log("Marker1");
if (string.IsNullOrEmpty(w.error))
{
// Success
Debug.Log("Marker2");
//Removed working code here to populate popuplist, not relevant for problem
}
else
{
// Fail
Debug.Log("Marker3");
//Removed working code here, not relevant for problem
}
w.Dispose();
w = null;
}
Had a few problems myself not explictly holding the refernce to that class and not disposing when finished either.
Everything else seems fine though. Added a more explicit check for w.error too as I think error can sometimes be an empty string rather than null.
Answer by alankemp · Mar 16, 2017 at 02:40 PM
Are you destroying the game object that your GetAllLocations script is attached to?
Coroutines are owned by the game object that starts them, if the game object is destroyed the coroutine will stop.
Your answer
Follow this Question
Related Questions
Coroutine doesn't continue after yield another coroutine 0 Answers
Coroutine: delay transform rotation but start transform movement immediately 2 Answers
Designing a dialogbox that displays message, returns a custom object and waits for user response 0 Answers
Coroutines Madness (); 1 Answer
Invoke method is not called 2 Answers