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?
1 Answer
1
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.
Instantiate a pool up front and take items from it....
– whydoidoitI 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?
– MountDoomTeamThere's a lot of reflection going on serializing the components on a game object too... It would be better just to reuse them.
– whydoidoit