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
Answer by rob11 · May 21, 2019 at 01:02 AM
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
Well I don't have anything to add yet but I'm having the exact same problem: In a dungeon generator a room is trying to instantiate a prefab from the prefab it comes from and sometimes gets null. In my case if I try to generate several times I get success but thats not correct either. I'll keep looking.
Answer by FabDynamic · Oct 17, 2019 at 07:05 PM
@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.
This looks as wrong as it can get. $$anonymous$$onoBehaviours are runtime scripts. You can not mix runtimecode and editor code. If this is an editor script it shouldn't be a $$anonymous$$onoBehaviour.
In the OP's question and also in your comment above it's completely unclear where you got your prefab reference from. Note that you can not have a prefab reference to the own prefab in a script on that prefab. That's simply because when a prefab gets instantiated any self references are replaced with references to the instantiated version. That is necessary to keep child and component references within the prefab consistent. However that means if you add a prefab ot a variable on it's own prefab, after instantiation that reference will no longer reference the prefab but the instance.
Answer by unity_qT8Y9osPinb1jA · Mar 10 at 07:51 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612201758im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
can't save data in prefab and getting an error while press "Apply" in prefab! 0 Answers
Instantiation Problem 0 Answers
can't save data in prefab and getting an error while press "Apply" in prefab! 0 Answers
Not sure why i'm getting NullReferenceException 0 Answers
Object reference not set to an instance of an object C# 0 Answers