- Home /
Coroutine start in while
I want to update my scene every 5 minutes. I am updating colors and textures. For colors is working perfect but for textures no.
If I call UpdateTexture
without StartCoroutine
nothing happens. Is i delete the StopCoroutine
statement the computer is going crazy : memory increases until it reaches 3.7 GB and unity will crash due to 32 bit limitations. How can i update my texture without the "crazy" part ?
The code i use is pasted below. Thanks in advance for your ideas.
using UnityEngine;
using System.Collections;
using System.ServiceModel;
using UnityStoreLibrary;
public class UpdateScene : MonoBehaviour {
void Start(){
StartCoroutine(UpdateMyScene());
}
IEnumerator UpdateTexture(ItemFieldValue ifv, GameObject o) {
if (!o.renderer.material.mainTexture.name.Equals(ifv.value)){
string url = "";
if (!System.IO.File.Exists(Strings.defaultUserDirectory + ifv.value))
url = Strings.texturesHost + ifv.value;
else
url = "file://" + Strings.defaultUserDirectory + ifv.value;
WWW www = new WWW (url);
yield return www;
if (!System.IO.File.Exists(Strings.defaultUserDirectory + ifv.value))
SaveTextureToFile(www.texture, ifv.value);
o.renderer.material.mainTexture = www.texture;
}
StopCoroutine("UpdateTexture"); //should i use this here ?
//yield return null;
}
private IEnumerator UpdateMyScene(){
while(true){
UnityStoreClient client = new UnityStoreClient(new BasicHttpBinding(), new EndpointAddress(Strings.webserviceUrl));
Item[] items = client.GetUnity3dItems(null);
for(int i=0;i<items.Length;i++){
GameObject curent = GameObject.Find(items[i].name);
if (curent != null){
ItemFieldValue[] values = items[i].item_field_values;
for(int j=0;j<values.Length;j++){
GameObject o = GameObject.Find(curent.name + "/" + values[j].field_details.name);
if (o != null){
if (values[j].field_details.type_id.Equals(Strings.colorType)){
UpdateColor (values[j], o);
}
if (values[j].field_details.type_id.Equals(Strings.textureType)){
StartCoroutine(UpdateTexture(values[j], o));
}
}
}
}
}
yield return new WaitForSeconds(Strings.updateInterval);
}
}
}
Answer by AntiLunchBox · Jun 14, 2013 at 02:34 PM
Well you shouldn't need to put StopCoroutine at the end of UpdateTexture. If the absence of that is making it crash, I would Debug out how often UpdateTexture is getting called. It's inside that for-loop so its probably called quite a few times, jacking up your memory as it tries to get those textures.
It's probably not working because stop coroutine is stopping other instances of UpdateTexture that are running. That yield return www is probably taking a while so it may be better to just restructure your code.
First, you could have UpdateMyScene as a InvokeRepeating method.
Then you can have UpdateTexture(s) take in the ItemFieldValue array as a parameter instead, and have it do the looping itself to get the textures--so you only have to call it once (every 5 minutes).
I will try and rearrange my code and come back with a status. Thanks for the idea.
If it still is jacking up your memory regardless, you should try and control the format the texture comes in as, using LoadImageIntoTexture
Thanks a lot. Your response solved my problem. $$anonymous$$y processor is at 13% and the memory increases by 300 $$anonymous$$B but it's O$$anonymous$$. I will try LoadImageIntoTexture
.
Answer by create3dgames · Jun 14, 2013 at 10:42 PM
Actually, a very simple solution would be to just use Fixed Update and set Edit > Project Settings > Time > Fixed Timestep to 300.
The player is not moving anymore if i use this method. $$anonymous$$aybe he uses the fixedupdate
too.
you don't want to try that. It's basically making fixed update run every 300 seconds. What went wrong before?
Assu$$anonymous$$g that you don't use FixedUpdate
for things like player movement, it should work fine. Use regular Update
for player moving.
FixedUpdate is used to also set off trigger events. If he's using any triggers it could have dramatic effects on his game.
I implemented the idea from AntiLunchbox
. I tried your idea first, create3dgames
because it looked simpler but i had problems with it. Sorry. I stick to the first answer.
Your answer
Follow this Question
Related Questions
I can't start a coroutine. I get a weird message from visual studio 1 Answer
Had difficulties implementing intro to Coroutines from unitypatterns.com. Help? 1 Answer
Coroutine sequence not running properly 1 Answer
Coroutine not working - what I did wrong? 2 Answers
Why does this co-routine not function properly after re-entering the scene? 0 Answers