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*.name);*
  •  		if (curent != null){*
    

ItemFieldValue values = items*.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);_
_
}_
_
}_
_
}*_

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).

Actually, a very simple solution would be to just use Fixed Update and set Edit > Project Settings > Time > Fixed Timestep to 300.