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 Issue with prefab variants

Discussion in 'Scripting' started by MrZeker, Jul 18, 2023.

  1. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    Hello

    I'm trying to make prefab variants at runtime in the editor and I cannot seem to make it work.

    What I want to do is:
    I have a basic enemy prefab, and I want all other enemies to be prefab variants of this one (so if I have to change stuff I only do it in the main prefab and not on every enemy prefab).
    I also have a very robust character creator, which I modified to be able to work with my enemy prefabs, so I can customize their appereance there and then save them.
    However when I save them they are saved as a new prefab rather than as a prefab variant, and I couldn't find any other solution to this.
    At this point I'm happy with the possibility to not create the new variant, but just be able to edit one prefab variant and store the changes made at runtime in the editor, but that doesn't seem possible iether.

    This is the code that I tried:
    (solution form this script)
    https://forum.unity.com/threads/make-prefab-variant-from-code.1063577/
    and also from
    https://forum.unity.com/threads/solved-creating-prefab-variant-with-script.546358/
    (I run the first part on Awake(), since other scripts use the Start() to set some stuff for the customization)
    Code (CSharp):
    1.  
    2.             string originalPrefabPath = AssetDatabase.GetAssetPath(CharacterPrefab);
    3.             Object originalPrefab = (GameObject)AssetDatabase.LoadAssetAtPath(originalPrefabPath, typeof(GameObject));
    4.             instance = PrefabUtility.InstantiatePrefab(originalPrefab) as GameObject;
    5.  
    6.  
    And then I run this to save
    Code (CSharp):
    1.  
    2. if (!Directory.Exists("Assets/Prefabs"))
    3.                 AssetDatabase.CreateFolder("Assets", "Prefabs");
    4.             string localPath = "Assets/Prefabs/NPC/" + NPCName + ".prefab";
    5.             GameObject prefabVariant = PrefabUtility.SaveAsPrefabAsset(instance, localPath);
    6.  
    Which results in creating a new prefab, and then variants of this new prefab, but that is not what I need, and I cannot for the life of me make 200 enemies in one sitting.

    Any idea how I could make this work?
    I tried several other variations and ideas, including "PrefabUtility.ConnectGameObjectToPrefab" and "Prefabutility.SaveAndConnect" or something like that to no avail.

    Any help is appreciated as I'm stuck here.
    Im on unity 2021.3.27f LTS
     
  2. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    Not sure this solves your problem but it seems to work.

    Code (CSharp):
    1.  
    2. GameObject prefabRef = (GameObject)AssetDatabase.LoadMainAssetAtPath("Assets/Prefabs/RedGem.prefab");
    3.         GameObject instanceRoot = (GameObject)PrefabUtility.InstantiatePrefab(prefabRef);
    4.         instanceRoot.AddComponent<Rigidbody>(); // just for testing;
    5.         GameObject pVariant = PrefabUtility.SaveAsPrefabAsset(instanceRoot, "Assets/Prefabs/RedGemVariant2.prefab");
    This was posted on StackOverflow (https://stackoverflow.com/questions/53095501/how-can-i-create-a-prefab-variant-in-c-sharp-script.) New variants are created, they have the original prefab as prefab(parent), and follow for example a dimension change of the prefab(parent).
     
  3. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    this didn't work either, it still creates new prefabs that are not variants, the only thing I did differently is separate the script, as I load the prefab (steps 2,3) on Awake(), then modify it, and then save it (step 5).
    But it did create a new non-variant prefab
     
  4. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    How about you try it just the way it is shown. Just to have a, hopefully, starting point.
    Are you sure it creates a new prefab that is not a vairant?

    This is what my variant created by script looks like
    upload_2023-7-18_18-48-28.png
     
  5. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    Yeah.
    I will make a video when I come back, but basically I separate it in 2 parts because after instanciating the prefab, I modify it using sliders and stuff in play mode, so once I'm done with the modifications I run the saving part.
     
  6. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    this image is better.
    Okay, looking forward to that.

    This image is better btw
    upload_2023-7-18_19-41-57.png
     
  7. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    There we go:


    I'm sorry for my bad english I'm not a native english speaker.
     
  8. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    I don't know how to mark it as solved, but I got it working, turns out the problem was that I was calling the first part in Awake (so it didn't had issues with other scripts). Calling it in Start() fixed it
     
  9. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    Congratz on solving it. Maybe just add [Resolved] to the title.