Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by StephanK · Sep 18, 2010 at 03:04 PM · prefabcrashasset

How to create prefabs from Editor scripts?

I want to have a custom menu item that creates a prefab for me. I tried doing this:

public class ImportAnimations : ScriptableWizard {

 public Texture2D spritesheet;
 public TextAsset spritedoc;

 [MenuItem("Custom/Animations/ImportAnimations...")]
 static void CreateWizard()
 {
     ScriptableWizard.DisplayWizard("Import Animations from:", typeof(ImportAnimations), "Import");  
 }

 void OnWizardCreate()
 {
     GameObject go = new GameObject("TestAsset");
     go.AddComponent(typeof(Animation));
     go.SetActiveRecursively(false);
     AssetDatabase.CreateAsset(go, "Assets/Prefabs/TestAsset.prefab");
     GameObject.DestroyImmediate(go);
 }

}

This creates the prefab, but it also keeps the GameObject in the scene. If I delete the GameObject the prefab will be empty (the Animation component is lost). If I delete the prefab from my project folder before deleting the GameObject in my scene Unity crashes. (reported bug) Is there another way to do this? Or is it just a bug? (using b7)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
14

Answer by StephanK · Sep 18, 2010 at 03:43 PM

After searching a bit more I found this answer which solved my problem:

http://answers.unity3d.com/questions/1125/how-do-i-programmatically-assign-a-gameobject-to-a-prefab

Keeping the question here, so the next one who's as dumb as I will have a better chance of finding the right answer.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Anxo · Jul 17, 2014 at 11:03 PM 0
Share

404! Whats the answer?

avatar image ashjack · Aug 21, 2014 at 03:33 PM 0
Share

http://answers.unity3d.com/questions/8633/how-do-i-programmatically-assign-a-gameobject-to-a.html

avatar image JeromeJ · Mar 06, 2018 at 08:35 PM 0
Share

Shouldn't this answer be marked as duplicate ins$$anonymous$$d?

avatar image
6
Wiki

Answer by intrepidis · Nov 19, 2014 at 01:54 AM

This way works.

 // Create some asset folders.
 AssetDatabase.CreateFolder("Assets/Meshes", "MyMeshes");
 AssetDatabase.CreateFolder("Assets/Prefabs", "MyPrefabs");

 // The paths to the mesh/prefab assets.
 string meshPath = "Assets/Meshes/MyMeshes/MyMesh01.mesh";
 string prefabPath = "Assets/Prefabs/MyPrefabs/MyPrefab01.prefab";
 
 // Delete the assets if they already exist.
 AssetDatabase.DeleteAsset(meshPath);
 AssetDatabase.DeleteAsset(prefabPath);

 // Create the mesh somehow.
 Mesh mesh = GetMyMesh();

 // Save the mesh as an asset.
 AssetDatabase.CreateAsset(mesh, meshPath);
 AssetDatabase.SaveAssets();
 AssetDatabase.Refresh();
 
 // Create a transform somehow, using the mesh that was previously saved.
 Transform trans = GetMyTransform(mesh);
 
 // Save the transform's GameObject as a prefab asset.
 PrefabUtility.CreatePrefab(prefabPath, trans.gameObject);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Storing a part of a complex asset in an asset bundle 1 Answer

Show and Hide a prefab or GameObject 10 Answers

cloned game object wont call script right... 2 Answers

Maintaining Maya binary prefab connection 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges