- Home /
Bool returns different values in Update and OnGUI
Hello, i'm having a problem that's giving me headache :
when i check the value of a bool in Update, it returns False, but in OnGUI i get a True. Here's the code which contains different variables i added for checking the returns, among which the int "count" that only counts up to 139 then stops for some reason.
public class LoadGallery : MonoBehaviour {
public Params pars = null;
public List<GameObject> galleryObjectsToSpawn = null;
public GameObject[] galleryFrameworksToSpawn = null;
private GameObject[] galleryObjects = null;
private WWW tmp = null;
private bool replaced = false;
private string output = null;
private string done = null;
private int count = 0;
void Start () {
Debug.Log("Starting");
pars = (Params)FindObjectOfType(typeof(Params));
foreach (var item in galleryObjectsToSpawn)
{
if (Instantiate(item) == null)
Debug.Log("Error : couldn't create " + item.name);
}
galleryObjects = new GameObject[galleryFrameworksToSpawn.Length];
GameObject obj;
for (int i = 0; i < galleryFrameworksToSpawn.Length; i++)
{
if ((obj = (GameObject)Instantiate(galleryFrameworksToSpawn[i])) == null)
Debug.Log("Error : couldn't create " + galleryFrameworksToSpawn[i].name);
else
{
galleryObjects[i] = obj;
StartCoroutine(pars.GetAssetBundle(pars.Country + ".unity3d"));
StartCoroutine(pars.GetTexture("Mini/" + pars.Country + "/" + pars.Country.ToLower() + "_" + (i + 1).ToString() + "__main_mini.jpg", obj));
}
}
Debug.Log("Done");
}
void Update()
{
if (tmp == null)
{
tmp = pars.Assets[pars.Country + ".unity3d"];
output = tmp.url;
}
else if (tmp.isDone && !replaced)
{
for (int i = 0; i < galleryObjects.Length; i++)
{
galleryObjects[i].renderer.material.mainTexture = (Texture2D)Instantiate(tmp.assetBundle.Load(pars.Country.ToLower() + "_" + (i + 1).ToString() + "__main.jpg"));
galleryObjects[i].transform.Rotate(new Vector3(0, 0, 45));
output = (i + 1).ToString() + " - " + galleryObjects[i].renderer.material.mainTexture;
}
//tmp.Dispose();
//pars.Assets.Remove(pars.Country + ".unity3d");
replaced = true;
}
if (tmp != null)
{
done = tmp.isDone.ToString();
count += 1;
}
}
void OnGUI()
{
if (tmp != null)
{
if (tmp.isDone)
GUI.Label(new Rect(0, 100, 500, 25), "Done loading the images " + tmp.isDone + " and replacing " + replaced + " count " + count);
else if (tmp.error != null)
GUI.Label(new Rect(0, 100, 500, 25), tmp.error);
else
GUI.Label(new Rect(0, 100, 500, 25), (tmp.progress * 100).ToString() + "%");
}
if (output != null)
GUI.Label(new Rect(0, 200, 500, 25), output);
if (done != null)
GUI.Label(new Rect(0, 225, 500, 25), done);
}
}
The bool giving different results is tmp.isDone. On every try it shows the "Done loading the images" message, but won't go into the loop to place them on the objects. Also, i use another script for downloading bundles :
private Dictionary<string, WWW> abDic = new Dictionary<string, WWW>();
public Dictionary<string, WWW> Assets
{
get { return abDic; }
}
public IEnumerator GetAssetBundle(string url)
{
WWW ab;
abDic.Add(url, (ab = new WWW(url)));
yield return ab;
}
the dictionary being private is to avoid it being showed in the Editor. Does anyone have an idea as to why the bool is returning different values?
Ok, here's some additional info about the script's purpose and the infos i got after making other tests :
The goal of the script is to load thumbs/smaller version images (called $$anonymous$$i), so the user will be able to see the images till the AssetBundle is downloaded and the $$anonymous$$is are replaced with high quality images.
The part that's not working is replacing the $$anonymous$$is with the high quality images once the asset has finished downloading.
Since it's searching for local files as a WebPlayer, i didn't know how make it search for the files in editor so i was testing with an emulated server with EasyPHP and building the player to test every change.
When i tried the player in editor, of course it didn't find the files but tmp.isDone was giving the right value and didn't stop updating after a few seconds, contrary to the built player.
Answer by ks13 · Dec 22, 2011 at 02:18 PM
So, not sure if it's being drowsy this morning or due to fatigue but i totaly missed the big line in the middle of the loop :
StartCoroutine(pars.GetAssetBundle(pars.Country + ".unity3d"));
Since it's the hihg quality images assetBundle, calling it's downloading more than once is weird, and pretty taxing on the system. After i moved it out of the loop the bool went back to giving correct values. What i don't understand is why it worked in the editor. Anyway, if anyone else got this problem, it might be the same case as mine.