Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by laszlar · Jun 03, 2016 at 06:00 PM · scripting problemscorepreformance

Performance Issue on Update

I'm having trouble figuring this out. I'm trying to slow the speed of an object depending on another script [the score]. Problem is that performance is really bad, and doing a Coroutine isn't very practical since checking the score every X amount of time - might actually not work all the time.. I.E. What's the chance of hitting the score on the 5th second that the Coroutine is ran?

Here is my script:

 public class MovePlank : MonoBehaviour
 {
     public float plankMover = -2.0f;
     ScoreTracker checkScore;
 
     public float amplitudeX = 10.0f;
     public float amplitudeY = 5.0f;
     public float omegaX = 1.0f;
     public float omegaY = 5.0f;
     float index;
     float checkScoreTime = 5.0f;
     bool checkLevelOne = false;
 
 
     // Use this for initialization
     void Start ()
     {
         checkScore = GetComponent<ScoreTracker>();
     }
     
     // Update is called once per frame
     void Update ()
     {
         StartCoroutine(WaitToCheckScore(checkScoreTime));
 
         transform.Translate(plankMover * Time.deltaTime, 0f, 0f);
 
         if (checkLevelOne)
         {
             /* index += Time.deltaTime / plankMover; //this changes the X speed as it technically slows down time. Increase paintSpeed to slow it down further.
              float x = amplitudeX * Mathf.Cos(omegaX * index); //amplitude X is the X value as where it'll generate while omegaX shouldn't really be touched. 
              float y = amplitudeY * Mathf.Sin(omegaY * index); // amplitude Y is the Y value as where it'll generate and omegaY is how many times it goes up and down.
              transform.localPosition = new Vector3(x, y, 0); */
              transform.Translate(plankMover / 2 * Time.deltaTime, 0f, 0f);
         }
     }
 
     IEnumerator WaitToCheckScore (float checkScoreTime)
     {
         yield return new WaitForSeconds(checkScoreTime);
         if (checkScore.levelOne)
         {
             checkLevelOne = true;
         }
     }
 }

Suggestions?

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

1 Reply

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

Answer by Chris333 · Jun 03, 2016 at 08:03 PM

Hi,

your performance problems are maybe occuring because you are starting each frame a new coroutine.

Try to start the coroutine only if its not already running. Something like that:

 if(!running)
 {
     StartCoroutine(WaitToCheckScore(checkScoreTime));
 }
 
 IEnumerator WaitToCheckScore (float checkScoreTime)
  {
      running = true
      yield return new WaitForSeconds(checkScoreTime);
      running = false
      if (checkScore.levelOne)
      {
          checkLevelOne = true;
      }
  }
Comment
Add comment · Show 2 · 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 laszlar · Jun 03, 2016 at 08:36 PM 0
Share

@Chris333 I didn't really want to use a Coroutine though. It was just the only way I can think of that limits in checking if something is true and not bog down the performance. Is this the only way, by using a coroutine?

avatar image laszlar · Jun 03, 2016 at 08:54 PM 0
Share

@Chris333 Alright, so I do need to use a coroutine. Thank you very much for your help!

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

66 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 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 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 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

Hi, we are currently doing our thesis, it's a quiz type of game. My question is, how do I collect the scores from four different scripts and output them on the menu page? 0 Answers

I need help with my script please 1 Answer

Help to Add some Sort and Binary Search in the code. 1 Answer

always the same problem, how to add 1 valor correctly not overtime ? 0 Answers

Easy Short kill score script not working 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