Prefabutility.instantiateprefab returns null SOMETIMES.

Is there any reasons instantiateprefab would return null in some rare case in my room generator script ? It was working perfectly fine with Instantiate() but since I wanted to keep the prefab connection, I swapped everything to prefabutility.instantiateprefab(). It works as before, but sometimes, the instantiateprefab would return null. I tried to find a similar case but nothing would solve my problem.

 private void SpawningTheRoom()
    {
        if (roomManager.MaxRooms > roomManager.RoomCount && roomManager.RoomGrid[spawnerX, spawnerZ] == GridInfo.NA)
        {
            SeeNewRoomRequirements();
            spawnedARoom = true;
            if (mustHaveWestE)
            {
                if (mustHaveNorthE)
                {
                    if (mustHaveEastE)
                    {
                        if (mustHaveSouthE)
                        {
                            room = PrefabUtility.InstantiatePrefab(roomEEEE.gameObject) as GameObject;
                            if (!room)
                            {
                                print("no ee");
                            }
                            room.transform.position = transform.position;                                                       
                        }
                        else
                        {
                            index = Random.Range(0, roomsWNEE.Length);
                            room = PrefabUtility.InstantiatePrefab(roomsWNEE[index].gameObject) as GameObject;
                            if (!room)
                            {
                                print(index);
                            }
                            room.transform.position = transform.position;                            
                        }
                    }

So here

Okay, after reviewing this problem for another hour, I found something interesting. The null reference exception happens when a room tries to instantiate a room from the same prefab as its prefab. Can

@rob11 Ok I got it working consistently, reliably, and without errors. Basically you get the path and then load from it and then instantiate from that. Its a bit of a funky workaround though so if anyone has a better way lets hear it.

Call PrefabFix.Instantiate instead of prefabUtility.InstantiatePrefab:

using UnityEngine;
using UnityEditor;

public class PrefabFix : MonoBehaviour
{
    public static GameObject Instantiate(GameObject prefab)
    {
        // This works reliably
        string assetpath = PrefabUtility.
            GetPrefabAssetPathOfNearestInstanceRoot(prefab);
        GameObject go =
            AssetDatabase.LoadAssetAtPath(
                assetpath, typeof(GameObject))
                as GameObject;
        return PrefabUtility.InstantiatePrefab(go) as GameObject;

    }
}

As you mentioned in your comment it only seems to happen for me when I try to instantiate a prefab from code running in a component of another instance of the same prefab (or something like that).
Another thought is I haven’t tried caching ‘go’ and instantiating from that for all future rooms.

I’m having the same issue with Prefabutility.instantiateprefab returning null.

Personally, I just wish this worked more similarly to Instantiate, with the same parameters.

Or maybe Instantiate could accept a true/false parameter that preserves the prefab connection if there’s one.