Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Make objects spawn faster

Discussion in 'Scripting' started by Deivydas4, Aug 30, 2023.

  1. Deivydas4

    Deivydas4

    Joined:
    Oct 28, 2021
    Posts:
    16
    Hey!
    I encountered a question, is it possible to make a Coroutine or maybe not to spawn objects fast :D

    Code (CSharp):
    1.  
    2.     public IEnumerator GenerateMeshFromVertices()
    3.     {
    4.         var verticesData = vertices.Select(x => x.Coordinates).ToArray();
    5.         int count = 0;
    6.  
    7.         var rust0 = infrastructure.transform.Find("RustObjects").transform.Find("rust_0").gameObject;
    8.         var rust1 = infrastructure.transform.Find("RustObjects").transform.Find("rust_1").gameObject;
    9.         var rust2 = infrastructure.transform.Find("RustObjects").transform.Find("rust_2").gameObject;
    10.         var rust3 = infrastructure.transform.Find("RustObjects").transform.Find("rust_3").gameObject;
    11.         var rust4 = infrastructure.transform.Find("RustObjects").transform.Find("rust_4").gameObject;
    12.         var rust5 = infrastructure.transform.Find("RustObjects").transform.Find("rust_5").gameObject;
    13.         var rust6 = infrastructure.transform.Find("RustObjects").transform.Find("rust_6").gameObject;
    14.         var rust7 = infrastructure.transform.Find("RustObjects").transform.Find("rust_7").gameObject;
    15.         var rust8 = infrastructure.transform.Find("RustObjects").transform.Find("rust_8").gameObject;
    16.         var rust9 = infrastructure.transform.Find("RustObjects").transform.Find("rust_9").gameObject;
    17.         var rust10 = infrastructure.transform.Find("RustObjects").transform.Find("rust_10").gameObject;
    18.         var rust11 = infrastructure.transform.Find("RustObjects").transform.Find("rust_11").gameObject;
    19.         var rust12 = infrastructure.transform.Find("RustObjects").transform.Find("rust_12").gameObject;
    20.  
    21.         foreach (var item in vertices)
    22.         {
    23.             var rust = rust0;
    24.  
    25.             if (item.CorrosionLevel >= -0.03f)
    26.                 rust = rust0;
    27.             else if (item.CorrosionLevel >= -0.07f)
    28.                 rust = rust1;
    29.             else if (item.CorrosionLevel >= -0.13f)
    30.                 rust = rust2;
    31.             else if (item.CorrosionLevel >= -0.18f)
    32.                 rust = rust3;
    33.             else if (item.CorrosionLevel >= -0.23f)
    34.                 rust = rust4;
    35.             else if (item.CorrosionLevel >= -0.27f)
    36.                 rust = rust5;
    37.             else if (item.CorrosionLevel >= -0.32f)
    38.                 rust = rust6;
    39.             else if (item.CorrosionLevel >= -0.37f)
    40.                 rust = rust7;
    41.             else if (item.CorrosionLevel >= -0.42f)
    42.                 rust = rust8;
    43.             else if (item.CorrosionLevel >= -0.47f)
    44.                 rust = rust9;
    45.             else if (item.CorrosionLevel >= -0.52f)
    46.                 rust = rust10;
    47.             else if (item.CorrosionLevel >= -0.57f)
    48.                 rust = rust11;
    49.             else
    50.                 rust = rust12;
    51.  
    52.             var copy = GameObject.Instantiate(rust, new Vector3(item.Coordinates.x, item.Coordinates.y, item.Coordinates.z), Quaternion.identity);
    53.             copy.transform.localScale = new Vector3(sphereSize, sphereSize, sphereSize);
    54.             copy.SetActive(true);
    55.             copy.name = "copy" + count.ToString();
    56.             copy.transform.SetParent(this.transform);
    57.             count++;
    58.  
    59.             yield return null;
    60.         }
    61.  
    62.         finish = true;
    63.     }
    64.  
    This is my IEnumerator which returns null. Is it possible to make it spawn points faster or I should use something else.

    PS. I want to spawn vertices which I get from 3D model, there is aprox. 45K vertices.

    Thank you!
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    transform hierarchy Find can be more simple:
    Code (CSharp):
    1. var rust0 = infrastructure.transform.Find("RustObjects/rust_0").gameObject;
    As you just search through hierarchy with an additional "/"
     
  3. Deivydas4

    Deivydas4

    Joined:
    Oct 28, 2021
    Posts:
    16
    I think maybe it's better to add to list and then from list take the value I need
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Ideally you don't use Transform.Find, but instead just serialise a list in the inspector, and assign reference through that.

    In any case,
    yield return null
    waits for the next frame. If you want this to happen faster, then you can just have a counter you increment with each run of the loop, and yield every n number of loop iterations.

    If you want it to happen all at once, then don't use a coroutine obviously.
     
  5. Deivydas4

    Deivydas4

    Joined:
    Oct 28, 2021
    Posts:
    16
    Yes yes, I think I will do that :)