- Home /
The question is answered, right answer was accepted
create prefab via script (JS)
Hi!
Ive tried many times, but i get errors and cant solve problem - create prefab from a gameObject.
Can someone help me out?
Here i add some gameObjects to a GameObject as children and after that i need to make a prefab out of that parent... I ve already tried
//add children snippet
modelShapes.objectSet[modelShapes.currentIdx].transform.parent=transform;
hairObjects.objectSet[hairObjects.currentIdx].transform.parent=transform;
//add prefab snippet
function MakePrefab()
{
var prefab : Object = EditorUtility.CreateEmptyPrefab ("Assets/createdPrefab.prefab");
EditorUtility.ReplacePrefab(gameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);
}
Thanks in advance! I guess it should also help other people, who are searching for solution to this problem.
So you want to DEFINE a prefab as if you would do it in javascript. Or do you want to instance a gameObject in your world?
I dont want to instantiate a prefab in world
i want to define a prefab and add gameObject to it, so i can use it later.
You can't use UnityEditor functions in a $$anonymous$$onoBehaviour script. Well, you can but you won't be able to create a build of your game. You need to place the script in an folder called "editor" and make sure you import the UnityEditor namespace. Use either the Selection class to access the desired GameObjects or create an EditorWindow / wizard.
Prefabs are an Editor only feature (at least until now). In the runtime the prefab information is gone.
Errors are not "blah blah". $$anonymous$$ost of time they tell you exactly what's wrong, you just have to read it carefully and if you don't understand it at least copy&paste it here.
with Blah Blah i meant that there is more text that i cant remember right now - not that its useless!
So if i solved the problem then it means that only works in Editor, not Game itself? If yes... How can then i save an Avatar - Ive made my AvatarCustomization system
Answer by Waz · Jul 17, 2011 at 12:31 PM
At runtime (which you seem to be talking about), a prefab is just the same as any other inactive GameObject. If you're talking about runtime, this is a no-op.
As to saving, you need to do that yourself at runtime. Search here for "save load" and you'll get heap of advice on that part.
I thought i could make a prefab and later in another Scene instantiate it if I need to.
You don't need to do anything special to do that. Provided the GameObject is marked DontDeleteOnLoad it will be there and you can Instantiate it to make a copy.