Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using Instantiate on a object that had children added to it on the same frame

Discussion in 'Scripting' started by Guedez, Jun 13, 2019.

  1. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    I am attempting to cache my xml generated objects. The issue is that the Instantiate is not working correctly
    It is not cloning the children objects that are added to it

    Code (CSharp):
    1. GameObject obj = new GameObject(path, typeof(RectTransform));
    2.         Component control = null;
    3.         if (controller != null) {
    4.             control = obj.AddComponent(controller);
    5.         }
    6.         XMLlayout.reparent = obj;
    7.         XMLlayout.ReloadXmlFile();
    8.  
    9.         List<Transform> list = new List<Transform>();
    10.         foreach (Transform t in XMLLoader.transform) {
    11.             list.Add(t);
    12.         }
    13.         foreach (Transform t in list) {
    14.             t.hideFlags = HideFlags.None;
    15.             t.parent = obj.transform;
    16.         }
    17.         XMLUtils.BindObject(obj);
    18.         if (typeof(UIController).IsAssignableFrom(controller)) {
    19.             ((UIController)control).OnLayoutRebuilt();
    20.         }
    21.         UnityEngine.Profiling.Profiler.EndSample();
    22.         //obj.hideFlags = HideFlags.HideAndDontSave;
    23.         return xmlCache[p] = Instantiate(obj);