Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rhose · Jun 14, 2013 at 02:13 PM · coroutinecoroutine errors

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);
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rhose · Jun 14, 2013 at 02:37 PM 0
Share

I will try and rearrange my code and come back with a status. Thanks for the idea.

avatar image AntiLunchBox · Jun 14, 2013 at 04:17 PM 0
Share

If it still is jacking up your memory regardless, you should try and control the format the texture comes in as, using LoadImageIntoTexture

avatar image rhose · Jun 17, 2013 at 02:58 PM 0
Share

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.

avatar image
0
Wiki

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rhose · Jun 17, 2013 at 07:41 AM 1
Share

The player is not moving anymore if i use this method. $$anonymous$$aybe he uses the fixedupdate too.

avatar image AntiLunchBox · Jun 17, 2013 at 01:11 PM 1
Share

you don't want to try that. It's basically making fixed update run every 300 seconds. What went wrong before?

avatar image create3dgames · Jun 17, 2013 at 05:53 PM 0
Share

Assu$$anonymous$$g that you don't use FixedUpdate for things like player movement, it should work fine. Use regular Update for player moving.

avatar image AntiLunchBox · Jun 17, 2013 at 06:10 PM 0
Share

FixedUpdate is used to also set off trigger events. If he's using any triggers it could have dramatic effects on his game.

avatar image rhose · Jun 17, 2013 at 06:16 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges