When adding components to prefabs some assets are not considering as prefabs. Why is that ?

using Packages.Rider.Editor.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using UnityEditor;
using UnityEngine;

public class AddComponents : Editor
{
    private static string GetClickedDirFullPath()
    {
        string clickedAssetGuid = Selection.assetGUIDs[0];
        string clickedPath = AssetDatabase.GUIDToAssetPath(clickedAssetGuid);
        string clickedPathFull = Path.Combine(Directory.GetCurrentDirectory(), clickedPath);

        FileAttributes attr = File.GetAttributes(clickedPathFull);
        return attr.HasFlag(FileAttributes.Directory) ? clickedPathFull : Path.GetDirectoryName(clickedPathFull);
    }

    private static string GetPath()
    {
        string path = GetClickedDirFullPath();
        int index = path.IndexOf("Assets");
        string result = path.Substring(index);

        return result;
    }

    static List<string> paths = new List<string>();
    [MenuItem("Assets/Get Prefabs")]
    private static void GetFolders()
    {
       
        string selectedPath = GetPath();

        string[] assetsPaths = AssetDatabase.GetAllAssetPaths();

        foreach (string assetPath in assetsPaths)
        {
            if (assetPath.Contains(selectedPath))
            {
                if (assetPath.Contains("Prefabs"))
                {
                    paths.Add(assetPath);
                }
            }
        }
    }
   

    [MenuItem("Assets/Get Prefabs")]
    public static void GetPrefabs()
    {
        GetFolders();

        for (int i = 0; i < paths.Count; i++)
        {
            if (File.Exists(paths[i]))
            {
                if (paths[i].Contains(".prefab"))
                {
                    GameObject contentsRoot = PrefabUtility.LoadPrefabContents(paths[i]);

                    if (contentsRoot.GetComponent<Rigidbody>() != null)
                    {
                        // Modify Prefab contents.
                        contentsRoot.AddComponent<Rigidbody>();

                        // Save contents back to Prefab Asset and unload contents.
                        PrefabUtility.SaveAsPrefabAsset(contentsRoot, paths[i]);
                        PrefabUtility.UnloadPrefabContents(contentsRoot);
                    }
                }
            }
        }
    }
}

With code I can make a right clock with the mouse on any folder in the Assets and then clicking on Get Prefabs and it will loop over all the folders and sub folders get all the prefabs and add a Rigidbody to them.

The problem is it’s adding the Rigidbody only to some of the prefabs.

For example in this screenshot to this prefabs it will add a Rigidbody :

but to prefabs I created by dragging objects to the Assets it’s not adding the Rigidbody component.
for some reason it’s not considering my prefabs as prefabs types or maybe the problem is something else.
I’m not sure what is the problem and how to fix it.

Screenshot of two of my prefabs :

This is a working script.

The main problem was that I checked if the prefab have a Rigidbody I did != null :

if (contentsRoot.GetComponent<Rigidbody>() != null)

but it should be if it’s null and then add a Rigidbody. The thing is that the prefabs in the first screenshot already have a Rigidbody I forgot that in my earlier tests and versions of the script I already added to them a Rigidbody so when it was checking !- null I thought by mistake it added a Rigidbody to the first prefabs but noy my prefabs. The fact is that it didn’t add any Rigidbody to any of the prefabs.

So the main changing is to check if it’s null if it’s == null and not != null :

var rb = contentsRoot.GetComponent<Rigidbody>();
if (rb == null)

Maybe the whole script could be changed to be easier and shorter and working better with a lot of prefabs but the way it is now it’s fine with me.

Anyway it’s now working just like I wanted.

using Packages.Rider.Editor.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using UnityEditor;
using UnityEngine;

public class AddComponents : Editor
{
    private static string GetClickedDirFullPath()
    {
        string clickedAssetGuid = Selection.assetGUIDs[0];
        string clickedPath = AssetDatabase.GUIDToAssetPath(clickedAssetGuid);
        string clickedPathFull = Path.Combine(Directory.GetCurrentDirectory(), clickedPath);

        FileAttributes attr = File.GetAttributes(clickedPathFull);
        return attr.HasFlag(FileAttributes.Directory) ? clickedPathFull : Path.GetDirectoryName(clickedPathFull);
    }

    private static string GetPath()
    {
        string path = GetClickedDirFullPath();
        int index = path.IndexOf("Assets");
        string result = path.Substring(index);

        return result;
    }

    static List<string> paths = new List<string>();
    [MenuItem("Assets/Get Prefabs")]
    private static void GetFolders()
    {
        string selectedPath = GetPath();
        string[] assetsPaths = AssetDatabase.GetAllAssetPaths();

        foreach (string assetPath in assetsPaths)
        {
            if (assetPath.Contains(selectedPath))
            {
                if (assetPath.Contains("Prefabs"))
                {
                    paths.Add(assetPath);
                }
            }
        }
    }

    [MenuItem("Assets/Get Prefabs")]
    public static void GetPrefabs()
    {
        GetFolders();

        for (int i = 0; i < paths.Count; i++)
        {
            if (File.Exists(paths[i]))
            {
                if (paths[i].EndsWith(".prefab"))
                {
                    GameObject contentsRoot = PrefabUtility.LoadPrefabContents(paths[i]);

                    var rb = contentsRoot.GetComponent<Rigidbody>();
                    if (rb == null)
                    {
                        // Modify Prefab contents.
                        contentsRoot.AddComponent<Rigidbody>();

                        // Save contents back to Prefab Asset and unload contents.
                        PrefabUtility.SaveAsPrefabAsset(contentsRoot, paths[i]);
                        PrefabUtility.UnloadPrefabContents(contentsRoot);
                    }
                }
            }
        }
    }
}