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
2
Question by pmccall2 · Apr 08, 2014 at 04:35 AM · c#updatecustomfixedupdate

C# Custom (slower) Update function?

I have a top down 2-d game, that generates new chunks as needed, depending on how close the player is to the current edge of the map. (Pseudo code below)

 //Begin Pseudo code
 
 
 int ChunkSize;
 
 //How far should I look to generate new chunks
 
 int ChunkRadius
 
 //The position of my current chunk
 
 Vector3 CurrentChunk;
 
 //A list of all my chunks
 
 List  ChunkPositions = new List();
 
 void PlayerToChunkCoords(Vector3 playerposition)
 {
     //Convert the players position to a set of chunk coordinates 
 
 }
 void CheckAndGenerateChunks()
 {
 //Get the chunk coordinates
 
 //use for loops and the radius to check if all of the chunks in range have been generated
 
 //if our list doesnt have a chunk within the radius, generate it and add its coordinates to the list
 }
 
 ///End pseudo code

This code doesn't need to run every frame, and putting it in fixedupdate (and slowing down fixedupate) gives me a huge gain in fps. But I really want to be able to take it out of fixedupdate, and put it on its own cycle.

Comment
Add comment · Show 1
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 Lo0NuhtiK · Apr 08, 2014 at 04:54 AM 0
Share

Use a Coroutine

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html

http://docs.unity3d.com/412/Documentation/ScriptReference/index.Coroutines_26_Yield.html

Also, when posting questions/answers/comments with code in them, use the 101/010 button to format it properly. Type/paste your code into your message, then highlight it all and press that button. I did it for you this time.

5 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by supernat · Apr 08, 2014 at 04:59 AM

If you need it to be consistently run at some frequency (and I mean consistent like it's always exactly every 1 second), then put it in FixedUpdate(), but wrap it in a "time to go" test like this:

 float timeToGo;

 void Start() {
     timeToGo = Time.fixedTime + 60.0f;
 }

 void FixedUpdate() {
     if (Time.fixedTime >= timeToGo) {
         // Do your thang
         timeToGo = Time.fixedTime + 60.0f;
     }
 }

In this example, it would run your code every 60 seconds.

You can alternatively, though less efficiently, use a coroutine like so:

 void Start() {
     StartCoroutine(work());
 }

 IEnumerator work() {
     while (true) {
         yield return new WaitForSeconds(60.0f);
         // Do some work
     }
 }

This will be a bit less efficient, because you have the overhead of the coroutine mixed in with a few extra cycles to yield for waitforseconds every frame.

Comment
Add comment · Show 4 · 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 Lo0NuhtiK · Apr 08, 2014 at 05:03 AM 0
Share

Or this also-> http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.InvokeRepeating.html

Or, or... or...

avatar image pmccall2 · Apr 08, 2014 at 07:44 AM 0
Share

Thanks, that first trick helped alot, I pretty much doubled the amount of chunks I can have (without lag).

Also, if anyone reads this in the future, if you have a big list of things, use a hashtable. At least in this instance the hashtable allowed me to add double the chunks over a list.

avatar image Bunny83 · Apr 08, 2014 at 11:00 AM 1
Share

@supernat: +1
However it makes no sense to pair FixedUpdate with Time.time since Time.time only changes between Update calls. Either use Time.fixedTime in FixedUpdate or use Time.time in Update.

avatar image supernat · Apr 08, 2014 at 02:50 PM 0
Share

@Bunny83, thanks for the correction, I will update.

avatar image
3

Answer by spraycanmansam · Apr 08, 2014 at 10:54 AM

You could also call InvokeRepeating("CheckAndGenerateChunks", 0, x) in your Start() function to call your code every x seconds.

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 supernat · Apr 08, 2014 at 02:55 PM 0
Share

Good answer, probably the cleanest way to get the results. I knew there was a method to do this but couldn't remember the name of it to save my life.

avatar image
3

Answer by Chrisdbhr · Aug 28, 2019 at 05:04 PM

Here is a simpler approach, using only one variable (the code inside Update will run every 3 frames in this example.):

 private int updateInterval = 3; // in frames
 
 void Update(){
     if (Time.frameCount % this.updateInterval != 0) return;
     
     // your code here...
 }


With this, you use only you extra variable on your code, and put just one extra line on your Update() loop.

The other reason that this is better than initializing Coroutines on Start() is because when you do this and your game object gets disabled and enabled again, you Coroutine will not run. Of course, you could initialize it OnEnable(), but it's better to just use 2 extra lines with only one new variable than allocate memory and processing time with coroutines.

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

Answer by Statement · Apr 08, 2014 at 11:21 AM

See this method of achieving a companion Update that you can call as often as you want.

While you may not be interested in being able to perform functions step-by-step you could modify CoStart to call your custom Update function every x seconds or every x events.

http://wiki.unity3d.com/index.php?title=CoUpdate

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

Answer by MoruganKodi · Nov 01, 2016 at 08:34 AM

You can have a constant update and chunk generation entirely using coroutines:

 void Awake()
 {
     StartCoroutine(ConstantUpdate);
 }
 
 IEnumerator ConstantUpdate()
 {
    while(true)
    {
         // come code
        if(m_IsGameRunning)
        {
            // wait for chunks to update
            yield return  UpdateChunks();
        }
        yield return null;
    }
 }
 
 IEnumerator UpdateChunks()
 {
      for(int i = 0; i < chunks.Count; i++)
     {
         // stuff
        yield return null;
     }
 }
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

29 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

Related Questions

Update and Fixed update function synchronicity 1 Answer

Checkibg Rigidbody2d and component problems 0 Answers

Fixed Update Problems C# - Please help! 1 Answer

Why Do Inputs Have to Been in Update (Rather than FixedUpdate)? 3 Answers

Multiple Cars not working 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