Search Unity

Editing thought script a prefabs variant

Discussion in 'Prefabs' started by usernameHed, Jul 3, 2019.

  1. usernameHed


    Apr 5, 2016
    Hello, here is my case:
    I have a prebabs "RaceTrack Main Prefab" who are the main prefabs,
    I have multiple "Race Track 1", "RaceTrack 2" who are variants of the RaceTrack Main Prefab.

    Now I am in my scene, here is the hierarchy:

    So, now I want in an editor script, to Delete, via a DestroyImmediate the gameObject Key_Drift_Manager for example. I have the RaceTrack 1 gameObject reference in the scene, and I want to delete the object Key_Drift_Manager, and save it automaticly into the RaceTrack 1 VARIANT.

    But As you know, I can't delete an object inside a prefabs from editor... so I have tryed to:
    - unpack RaceTrack 1
    - Delete my stuff
    - Save the new unpacked RaceTrack 1 to the Prefabs.

    I have manage to do all of that... Exept that my RaceTrack 1 became a PREFAB (and not a VARIANT anymore...)

    Here is the code:
    Code (CSharp):
    1. //get the "RaceTrack 1" GameObject
    2. GameObject rootPrefabs = UnityEditor.PrefabUtility.GetOutermostPrefabInstanceRoot(transform);
    3. string pathRootPrefabs = UnityEditor.PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(rootPrefabs);
    4. ...
    5. ... DestroyImediate my stuff
    6. ...
    7. UnityEditor.PrefabUtility.SaveAsPrefabAssetAndConnect(rootPrefabs, pathRootPrefabs, UnityEditor.InteractionMode.UserAction);

    So, How can I do for Deleting stuff from editor script inside a gameObject in a scene, who are a Prefabs Variant.

    Thank you !
  2. runevision


    Unity Technologies

    Nov 28, 2007
  3. usernameHed


    Apr 5, 2016
    I Have 100 levels, who are basicly the same, with only a spline inside who change, and some settings..
    I have made special tools for creating spline, like that I can change for each level the spline.

    So with your answer, that mean I have no solution for my problem ? if I want to have a tools who delete stuff in a scene, I just can't use prefabs....

    And if I don't use prefabs, and someday in the production, I want to change the architecture of one thing in one level, I have to change it in all the level... So I have to create even more custom tools... to simulate prefabs behavior.

    Please tell me you have another solution.
  4. Baste


    Jan 24, 2013
    There's the hack of having empty "container" objects in the base prefab that you can add objects to in the variants.

    That being said, deleting objects from prefabs and still keeping a connection seems to be the the #1 requested feature of prefabs, and one of the largest blockers people have when working with them, so I hope the team is strongly considering taking the time to implement it.