- Home /
How to create prefab from a GameObject variable in class?
I'm working on a NPC - System for my game and got this problem. I have a NPC class which contains some information about npc and PREFAB from .fbx model in it.
public class NPC
{
public int ID;
public string Name;
public string Description;
//Model mixiamo as prefab
public GameObject ItemPrefab;
public NPCFraction npcFraction;
}
Then I create some GUI to specify that data and assign some values. To the ItemPrefab I assign the prefab created from .fbx model. Later when I finish assigning data I want to create a new prefab from this class to allow my designers easy drag and drop. To do that I've created a script:
public class SaveObject : EditorWindow
{
public static void SaveAsPrefab(GameObject item, string path)
{
Object prefab = PrefabUtility.SaveAsPrefabAsset(item, path);
}
}
And now I try to save ItemPrefab (prevous original prefab from .fbx) as a new prefab with:
void save()
{
string path = "Assets/test.prefab";
SaveObject.SaveAsPrefab(_database.NpcList[0].ItemPrefab, path);
}
And Well... Error saying: ArgumentException: Can't save persistent object as a Prefab asset UnityEditor.PrefabUtility.SaveAsPrefabAssetArgumentCheck
Do anyone know what is happenning and how to fix it? Or Maybe suggest another solution, like saving whole class as a prefab?