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 11, 2013 at 09:48 AM · coroutineupdatestart

Update scene every 5 minutes

I would like to update some objects in my scene every 5 minutes. I tried doing it from the start function with a while(true) statement but the computer is going crazy and nothing gets played.

After reading a little about coroutines i changed the code. My code looks like :

 void Update () {
     if (Time.time > curent_time && sw){
     sw = false;
     UpdateMyScene();  //should i use `Invoke` with `0.0f` here ? 
     curent_time = Time.time + updateMinutes;
     sw = true;
     Debug.Log("New update at :" + Time.time + " Next update at : " + curent_time);
     }
 }  

 private IEnumerable UpdateMyScene(){
     Debug.LogWarning("In UpdateMyScene");
   [....]
   }

On the console i only get messages like the one in the update. Why don't i get the message from the function I call ?

Thansk in advance !

Comment
Add comment · Show 4
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 Benproductions1 · Jun 11, 2013 at 10:30 AM 0
Share

Have you tried looking up timers for Unity and tried some of the many examples?

avatar image rhose · Jun 11, 2013 at 10:56 AM 0
Share

I use Time.time. The problem is with the www coroutine.

avatar image $$anonymous$$ · Jun 11, 2013 at 11:06 AM 0
Share

It freezes unity when the coroutine start?

avatar image rhose · Jun 11, 2013 at 11:32 AM 0
Share

yes. I updated the code with my last version. any ideas

2 Replies

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

Answer by rhose · Jun 11, 2013 at 01:37 PM

 void Start(){
     StartCoroutine(UpdateMyScene());    
 }
 
 private IEnumerator UpdateMyScene(){
         while(true){
             Debug.Log("Time is now : " + Time.time);
             UnityStoreClient client = new UnityStoreClient(new BasicHttpBinding(), new EndpointAddress(Strings.webserviceUrl));
             Item[] items = client.GetUnity3dItems();
             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))
                                 UpdateTexture (values[j], o);    
                         }
                     }
                 }
             }
             yield return new WaitForSeconds(Strings.updateInterval);
         }
     }
Comment
Add comment · Show 1 · 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 11, 2013 at 01:29 PM 0
Share

this is the answer to my problem. a nice guy from unity chat helped me. thanks alot unity helpers.

avatar image
1

Answer by barker_s · Jun 11, 2013 at 12:11 PM

First of all, Start() fires only once at the beginning of your object's lifetime, so it's not a good idea to put any update code there. Putting while(true) in there is even worse, since you're never going to exit that method and it will hang in there indefinitely.

There's a different function where you can update your game objects and it's called... Update() :) .

Here's an example how you can do that:

 public float timeInterval; // interval in seconds
 float actualTime;
 
 void Start()
 {
    timeInterval = 300.0f; // set interval for 5 minutes
    actualTime = timeInterval; // set actual time left until next update
 }
 
 void Update()
 {
    actualTime -= Time.deltaTime; // subtract the time taken to render last frame
 
    if(actualTime <= 0) // if time runs out, do your update
    {
       // INSERT YOUR UPDATE CODE HERE
 
       actualTime = timeInterval; // reset the timer
    }
 }



I don't know if it's the right way to do that, but it should work all right.

Comment
Add comment · 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

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

Issues with InvokeRepeating in Start/Update functions 1 Answer

Coroutine not working properly on Android (C#) 0 Answers

Coroutines and if statements 1 Answer

How to slow down large code? 1 Answer

Why if I execute .SetText on the Start it dont works. 1 Answer


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