Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Create gameobject (terrain) in coroutine or asynchronously(?)

Discussion in 'Scripting' started by Wudek, Aug 10, 2012.

  1. Wudek

    Wudek

    Joined:
    Aug 10, 2012
    Posts:
    7
    Hey I've been struggling with this all week.

    I basically have a C# class that allows me to procedurally generate new terrains and to randomize these terrains at runtime as game objects move around in the world.

    However, whenever I need to create one (or more) of these terrain game objects, the game lags.

    I was thinking of creating/randomizing these terrain objects asynchronously so I started looking at coroutine and other async methods but it seems that unity complains if I try to create game objects in a different thread than the main one.

    Have any suggestions?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,503
    Coroutines still run in the main thread and are not asynchronous. They just allow you to spread instructions out over an amount of time or to wait for other instructions to complete.

    Indeed, Unity is not thread-safe. However, there are some things you can do outside of the main thread. If your creating a Mesh for instance you can create the vertex, normal, triangle, and uv lists in a separate thread and then do the assignments and creation of the actual Mesh itself in the main thread once that has completed.
     
unityunity