Search Unity

Question unity will crash when 1000gameobject set parent

Discussion in 'Scripting' started by ununion, Jun 17, 2021.

  1. ununion

    ununion

    Joined:
    Dec 2, 2018
    Posts:
    275
    Code (CSharp):
    1.  public GameObject prefab;
    2.     public int row = 30;
    3.  
    4.     [ContextMenu("create prefabs")]
    5.     public void createprefabs() {
    6.         for(int i = 0; i < row; i++) {
    7.             for(int j = 0; j < row; j++) {
    8.                 var g = Instantiate(prefab);
    9.                 g.transform.position = new Vector3(i, 0, j);
    10.                 g.transform.SetParent(transform);
    11.             }
    12.         }
    13.     }
    very simple code but will crash the project,why>?
     
  2. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    358
    You also instantiate, which is quite heavy operation.
    Set parent will probably force unity will to recalculate all space properties: adjust local position, local rotation and scale, which isn't free.
    Also, it might depend on your hardware.

    Anyway, the result is quite expectable, but if you need to actually create 1000 objects, then try to use coroutines. They should probably work in inspector. Just yield return null, after every row.
     
    ununion likes this.
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    30*30 objects is 600 objects. That should not in any way crash the project. It might incur a slight lag if the object is complex, but probably not.


    Is this a crash or a hang? If it's hanging, I suspect an infinite loop - if you call createprefabs from the Start or Awake method of a script attached to the prefab in question, you'd get an infinite loop.
     
    Joe-Censored likes this.
  4. ununion

    ununion

    Joined:
    Dec 2, 2018
    Posts:
    275
    its a crash on i9 9900k 16gbram computer,
    the code is quite simple like what i post,only instantiate the unity's cube
     
  5. ununion

    ununion

    Joined:
    Dec 2, 2018
    Posts:
    275
    thank you iwill try it
     
  6. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455

    Where in your code is reference for "transform" ??On line 10 you setting g as it child.

    Code (CSharp):
    1.  g.transform.SetParent(transform);
    You should have reference for it and set it in inspector.
    Code (CSharp):
    1. public Transform transform;
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Have you.... never used the Component.transform accessor?
     
  8. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,791
    A crash is expectable?
     
  9. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    No and same thinkg what is topic about is working fine for me.I instantiating abou 15k objects with same method and it is working fine


    Code (CSharp):
    1.  
    2. var mineral=Instantiate(CreateMineral(mineralPosition,materialIndex),terrainContainer.transform);
    3.  
     

    Attached Files:

    Last edited: Jun 21, 2021
  10. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    358
    I think yes, it's completley possible, and I wouldn't be so surpired if my Unity crashed after such stress-test.
     
  11. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    @Tekrel That's just wrong. You're off by a factor of a million with how much a modern computer should be able to handle. If you copy-paste OP's code and run it, it finishes instantly.

    If Unity crashed after instantiating less than one thousand objects, it wouldn't be able to load a single scene in a single project we or anyone else made. OP was for sure doing something busted.
     
    hippocoder likes this.
  12. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Probably needs to check console for null refs by the looks of it.
     
  13. ununion

    ununion

    Joined:
    Dec 2, 2018
    Posts:
    275
    the transform is localtransform in mono script which contains the method.
     
  14. ununion

    ununion

    Joined:
    Dec 2, 2018
    Posts:
    275
    crash both in 2020lts and 2019lts