GetPrefabAssetType returns incorrect value

Hi,

I’m currently trying to create a tool to speed up workflow. The function of this tool is to add component to a list of selected prefabs, but to prevent double adding on regular/variants, I want to check if the prefab I might have accidentally selected is already a variant.

Here come’s the problem, I am unable to get a return value of variant even though the asset I selected is a prefab variant, and I know it is because the icon of the prefab is with the black lines. Strangely if I create a variant of the variant, the engine returns me a value of variant instead of regular.

Recreating the scenario :
Create empty gameobject in scene;
Set gameobject as prefab;
Right click (prefab)gameobject and create prefab variant (gameobject variant);
Test code returns the (gameobject variant) as a regular prefab;
Right click (prefab)gameobject variant and create prefab variant (gameobject variant variant)
Test code returns the (gameobject variant variant) as variant prefab;

Here is the snippet of my code :

    public class ParticleCheckResult
    { 
        public GameObject myParSys;
        public string particleAddedResult;
    }
    public List<ParticleCheckResult> parSysForChecks = new List<ParticleCheckResult>();
    public void AddManaged()
    {
        foreach (var currPar in parSysForChecks)
        {
            GameObject assetRoot = currPar.myParSys as GameObject;
            string assetPath = AssetDatabase.GetAssetPath(assetRoot);
            GameObject contentsRoot = PrefabUtility.LoadPrefabContents(assetPath);
            Debug.Log(currPar.myParSys.name + " is " + PrefabUtility.GetPrefabAssetType(contentsRoot));
            continue;
    }

the GameObjects I placed in the list :
6655636--760672--Capture011.PNG

and the results :
6655636--760675--Capture012.PNG

as you can see, the base prefab is not even treated as a prefab, and the first level variant is treated as regular. Can someone enlighten me on whether this is intended behaviour, or should I use another method to check the prefab type? I’m using Unity 2019.3.9 if that should affect anything. Thanks!

You want to use PrefabUtility.GetPrefabAssetType(assetRoot), using the prefab asset (assetRoot) instead of its contents (contentsRoot).

What you’re getting the type of is the root object inside the prefab. With this, your results make sense: A regular prefab contains a non-prefab game object, a variant contains an instance of the original prefab and a variant of a variant contains an instance of the original variant.

1 Like

Thanks a bunch man, never thought I had to use the prefab as a GameObject rather than an asset.

Thanks for this important clarification @

As a side note while reading the above my head exploded.

But I understand it better now, so I got that going for me.