• 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 halimozturk · Apr 21, 2015 at 02:56 PM · updatelaggy

Using Coroutine on Update lagging a lot

I am making a checkpoint system for my game. Basically, I will pop-up an animated checkpoint image to user and they will understand that check point reached. I wanted to show that pop-up on every 10 scores. But my pop-up has to wait for 2 seconds and because of that I used yield WaitForSeconds and my method's return value became IEnumerator. So when I call the Coroutine on Update function, game is lagging insanely and also the code is not working. But if I call the Coroutine on Start function, pop-up appears on score=0.

How can I show my pop-up properly on every 10 scores without lagging.

So, Here is my code

 using UnityEngine;
 using System.Collections;
 
 public class Checkpoint : MonoBehaviour
 {
 
 
         public bool buttonState = false;    
         private Rect button = new Rect (Screen.width / 2, -100, Screen.width * 4 / 24, Screen.height / 9); //holds actual button rect coordinates
         private Rect initialPosition = new Rect (Screen.width / 2, -100, Screen.width * 4 / 24, Screen.height / 9); //holds starting rect coordinates
         private Rect activePosition = new Rect (Screen.width / 2 - Screen.width / 9, Screen.height / 5, Screen.width * 8 / 24, Screen.height / 6); //holds ending rect coordinates
         public Texture checkpointImage;
         private float remainder;
         private bool flag;
 
         void Start ()
         {
                 //For displaying popup on every 10 scores
                 remainder = Score.score % 10;
             
         }
 
         IEnumerator CheckPointDisplayer ()
         {
                 if (flag) {
                         yield return new WaitForSeconds (0.3f);    
                         iTween.ValueTo (gameObject, iTween.Hash ("from", button, "to", activePosition, "onupdate", "MoveButton", "easetype", "easeinoutback"));
                         yield return new WaitForSeconds (2f);    
                         iTween.ValueTo (gameObject, iTween.Hash ("from", button, "to", initialPosition, "onupdate", "MoveButton", "easetype", "easeinoutback"));
                         flag = false;        
                 }
         }
 
         void Update ()
         {
             // This is lagging a lot and also not working
                     // If I call exactly the code below, It works but only when score=0
                     // I want to make it work for every 10 scores like 10,20,30,40 ... etc.
                 if (remainder == 0) {
                         flag = true;
                         StartCoroutine (CheckPointDisplayer ());
                 }
         }
 
 
         void OnGUI ()
         {
     
                 GUI.DrawTexture (button, checkpointImage);
 
 
         }
     
         void MoveButton (Rect newCoordinates)
         {
                 button = newCoordinates;
         }
 
 
 }
Comment
Add comment · Show 3
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 Nerevar · Apr 21, 2015 at 02:59 PM 1
Share

maybe try :

   if (remainder == 0 && !flag) {
       flag = true;           
       StartCoroutine (CheckPointDisplayer ());
   }


and

  IEnumerator CheckPointDisplayer ()
          {
                  
                          yield return new WaitForSeconds (0.3f);    
                          iTween.ValueTo (gameObject, iTween.Hash ("from", button, "to", activePosition, "onupdate", "$$anonymous$$oveButton", "easetype", "easeinoutback"));
                          yield return new WaitForSeconds (2f);    
                          iTween.ValueTo (gameObject, iTween.Hash ("from", button, "to", initialPosition, "onupdate", "$$anonymous$$oveButton", "easetype", "easeinoutback"));
                          flag = false;        
                  
          }
avatar image Pharaoh_ · Apr 21, 2015 at 03:10 PM 1
Share

@Nerevar is correct. You need a boolean to stop relaunching the Coroutine constantly. However, you also need to use the remainder in the update. What you are doing right now is set the remainder on Start and in the update you always use that value to deter$$anonymous$$e the score count. Ins$$anonymous$$d, this: "remainder = Score.score % 10;" should be passed on the update method, right before the evaluation of == 0.

avatar image halimozturk · Apr 21, 2015 at 04:09 PM 0
Share

Thank you @Nerevar and @Pharaoh_ you are both right, using boolean flag stopped relaunching the Coroutine constantly and also passing "remainder = Score.score % 10;" on Update method provides me to pop-up the checkpoint on every 10 scores.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by itsmealex100 · Apr 21, 2015 at 04:22 PM

Okay so your main goal is to 'How can I show my pop-up properly on every 10 scores without lagging.'. Calling A coroutine in update is never a good idea if you can avoid it.
Personally I would try changing things around.

Create a function to Check if the players score is a multiple of 10 and then if true, run the coroutine, So:

 void CheckScore () {
 
  if(score % 2 == 0) {
 StartCoroutine (CheckPointDisplayer ());
 
 }

Then Simple call this function each time are increasing the players score. Sure its 1 extra line of code, but it will get run only when necessary instead of 60 times per second in the update function.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Laggy camera on const moving speed 1 Answer

Unity lagging after update? 2 Answers

Update function unstable frame rate 2 Answers

Game lags on iPhone in low power mode. 1 Answer

Extremely low fps in editor!!! 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