• 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 Neurological · 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 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 avatar image

Related Questions

Issues with InvokeRepeating in Start/Update functions 1 Answer

How to make a spawner wait for x seconds? 2 Answers

inconsistent results calculating the distance moved by an object 1 Answer

Script and Start function of script are running, but Update function is not? 0 Answers

I'm having trouble with a coroutine delay 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges