- Home /
Why does CreateEmptyPrefab return null?
I did this in a ScriptableWizard
script:
var prefab = PrefabUtility.CreateEmptyPrefab(path + "/test.prefab") as GameObject;
prefab.AddComponent<TestComponent>();
The test.prefab is created correctly in the path. However, but the reference to the instance prefab
is null. Why is this so? How can I then hold on to the reference of the Prefab and add a component to it?
Answer by numberkruncher · Apr 13, 2013 at 12:36 PM
The following documentation demonstrates how to achieve this by replacing the contents of the prefab after creation:
http://docs.unity3d.com/Documentation/ScriptReference/PrefabUtility.CreateEmptyPrefab.html
For example, in your scenario the following may be of use:
// Create and prepare your game object.
GameObject go = new GameObject("Name of GO");
go.AddComponent<TestComponent>();
// Create empty prefab and replace with content of `go`.
Object prefab = PrefabUtility.CreateEmptyPrefab(path + "/test.prefab");
PrefabUtility.ReplacePrefab(go, prefab, ReplacePrefabOptions.ConnectToPrefab);
Another solution is to use the PrefabUtility.CreatePrefab
function which is slightly simpler than above:
// Create and prepare your game object.
GameObject go = new GameObject("Name of GO");
go.AddComponent<TestComponent>();
// Create prefab from object.
PrefabUtility.CreatePrefab(path + "/test.prefab", go, ReplacePrefabOptions.ConnectToPrefab);
When manual modifications are made to your prefab (following creation) be sure to use `EditorUtility.SetDirty` so that such changes are persisted.
Thanks, but there is this error when I try to add the component:
AddComponent asking for invalid type
UnityEngine.GameObject:AddComponent(Type)
You have to add the component to your game object which is created before attempting to update the prefab. Generally you do not adjust prefab objects directly (except for $$anonymous$$or property changes which must be marked as dirty).
I think the problem is with the TestComponent a script being created "on the fly". So the editor script that I have actually created TestComponent and a prefab, and then attempts to add TestComponent to the prefab. The problem is unity editor needs to refresh the asset database before it could recognise the TestComponent.
However, even when AssetDatabase.Refresh() is called, there is another error:
Script TestComponent has not finished compilation yet. Please wait until compilation of the script has finished and try again.
UnityEngine.GameObject:AddComponent(String)
I think that you should assume that your editor script which dynamically creates scripts cannot interact with the created scripts. Unity will automatically recompile all scripts, not just the ones you create dynamically.
To get a "callback" so-to-speak you could also dynamically create an editor script and use the "InitializeOnLoad" attribute to execute your one-off "callback" logic. And then finally delete the editor "callback" script so that it doesn't execute in the future. This will of course cause a second round of recompilation which might not be desirable performance-wise.
I am not sure how else you will be able to achieve this. There might very well be a hidden or undocumented way to achieve this .
Your answer
Follow this Question
Related Questions
How should I make the folder structure when I create a plugin for Unity 1 Answer
Safely deleting a child of a prefab 1 Answer
Need to make a reference in a prefab to itself not to the created instance 2 Answers
Selecting a newly created empty prefab by an editor script 0 Answers
Can I create a child gameobject on a prefab with an editor script 2 Answers