Search Unity

How to nest prefabs to another with api?

Discussion in 'Prefabs' started by GearVR, Nov 9, 2018.

  1. GearVR

    GearVR

    Joined:
    Nov 13, 2015
    Posts:
    16
    I'm generating some GameObjects hierarchy procedurally. I.e.,

    with prefabs A, B, C.
    I generate a

    MyGameObjectRoot
    A instance 1
    A instance 2
    B instance 1
    B instance 2


    I use the prefab utility to write MyGameObjectRoot hierarchy to prefab. But all the children objects loose their association with their original prefabs?
     
  2. GearVR

    GearVR

    Joined:
    Nov 13, 2015
    Posts:
    16
    Yes, the new nested prefab. Why does the doc say "To use this method, you must first add a GameObject to an existing Prefab instance."?

    Should I be using the above function like this?

    for( int i=0; i<100; i++) {
    GameObject InstanceOfPrefabA = Instantiate( PrefabAInstance);
    ApplyAddedGameObject( InstanceOfPrefabA, "./MyGameObjectRootPrefab", InteractionMode.AutomatedAction);
    }

    I'm hoping the 100 instances of PrefabA each still retain knowledge that each came from PrefabA, so that I can modify later PrefabA and all the 100 instances in this hierarchy will also reflect the change.
     
  3. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    JeremiahJ likes this.
  4. JeremiahJ

    JeremiahJ

    Joined:
    Nov 1, 2016
    Posts:
    28
    This is exactly what I was searching for just now. Thank you @SteenLund for those links.
     
  5. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    This seems backwards ...

    If I am to make B a child of A, why not make A a prefab first and make B a child of that prefab?

    This is loads more intuitive...