• 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 MountDoomTeam · Apr 29, 2013 at 12:35 PM · loopinstantiation

Way of queuing multiple instantiations?

I have a loop that instantiates objects in objectManagerScript.js, and each object has its own individual meshGeneratorScript.js on it.

At the moment, it instantiates many Mesh generating objects simultaneously and it causes massive slowdowns, I want to pause the loop every time it instantiates one object, and include a line at the end of the instantiated object mesh generator code with sendsmessage.unpause_loop back to instantiation loop script.

it would more sense than WaitForSeconds which doesn't really work in the given scenario.

can I pause and continue a loop dynamically? how do I queue instantiations until current instantiation mesh is rendered?

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 whydoidoit · Apr 29, 2013 at 03:27 PM 0
Share

Instantiate a pool up front and take items from it....

avatar image MountDoomTeam · May 04, 2013 at 05:08 AM 0
Share

I had a think about pooling objects, in this scenario max 15 objects have to appear every few seconds... It makes sense to pool objects when they all have mesh, but if its empty objects with scripts that have to destroy and renew the mesh every time the object is moved, it should be perhaps faster to instantiate and destroy the entire object?

avatar image whydoidoit · May 04, 2013 at 07:44 AM 0
Share

There's a lot of reflection going on serializing the components on a game object too... It would be better just to reuse them.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by cagezero · Apr 29, 2013 at 02:53 PM

There are a lot of ways you can accomplish what you want. I will go over two of them.

Option 1: Put your loop into a corotuine with this kind of logic (pseudo code):

 IEnumerator CreateObjects() {
 
     while(!enoughObjectsExist) {
         instantiate object
         yield return null
     }
 }

This will allow you to make one object per frame instead of making all the objects in one frame. Unless your object mesh generator code is somehow putting off the work till later there is no need to use send message. If that works for you, great! If not, queue option 2.

Option 2: Create a function that creates objects. No loop. Instead, allow the object to check if you currently have enough objects made (pseudo code).

 public void CreateObject() {
     if(enoughObjectsExist)
         return
     else
         instantiate new object
 }

Then you can use send message as you indicated, calling the CreateObject method as each mesh generation function is completed. Warning:

This option assumes that your object mesh generator code is somehow delayed, either by the object initially being inactive, put into some coroutine, etc. If not, you will see no performance difference from what you currently are doing.

Like I said these are just two example options. Depending on how your object mesh generator code is being called there might be other more efficient options available.

Comment
Add comment · Show 6 · 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 MountDoomTeam · Apr 29, 2013 at 04:46 PM 0
Share

hi that's cool, thanks! perhaps i will use an empty loop after instantiation that counts through fixed updates until it gets a send message from mesh being generated.

it's an environment gen, so it just generates missing spaces when the player moves, it can be any number of meshes, just i have to stagger the next instantiations until the previous are processed.

avatar image cagezero · Apr 29, 2013 at 05:18 PM 0
Share

@whydoidoit brings up a good point. For an environment gen I would bet that you can reuse parts of the environment that are currently not visible. If that is the case, creating a pool upfront to take items from would indeed be the best. You can then return unneeded items to the pool so that they can be reused later.

This would completely eli$$anonymous$$ate the necessity for creating new meshes every time the player moves!

avatar image MountDoomTeam · Apr 30, 2013 at 07:22 AM 0
Share

Hi, i have a terrain tiling code that switches around tiles that are out of view and its faster than instantiation, and that is what i will do with the marching cubes also after, it's just for testing , if i move all the meshes in one go it's slow. With marching cubes i have to erase the mesh and make a new mesh every time, and with tiles i just change the y height of vertices. for the moment it's just testing with instantiation and then will change with moveable objcts :) the difficulty is queing things in a loop/ pausing code when fps is slow.

avatar image MountDoomTeam · Apr 30, 2013 at 07:35 AM 0
Share

perhaps if i place this line in all the codes it would queue processing:

 for (var n:int = 0; n < 1000; n ++){
 if (  Time.deltaTime > 0.04 ){yield WaitForFixedUpdate(); }
 else break;

... except i cant use yield in the multithreading mesh code... :/

avatar image Bunny83 · Apr 30, 2013 at 07:52 AM 0
Share

@ZoomDomain: That doesn't work because Time.deltaTime as well as Time.time are constant during a frame. They only change in between frames. You have to use Time.realTimeSinceStartUp

Show more comments

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

15 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

Related Questions

Difference between while loop and dowhileloop? 1 Answer

Trigger audio loop on beat with PlayScheduled 1 Answer

problem with loop and mesh values 0 Answers

my foreach loop and condition wont work 0 Answers

Change transform.position.y in a loop 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