Search Unity

  1. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  2. 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,514
    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