Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
0
Question by HummeL_YT · Apr 29, 2021 at 10:52 PM · prefabgameobjectsconvertprefab-instance

How I can convert object to prefab instance?

I duplicated around 100-200 objects and then realized what just could make a prefab... But I have already configured those objects. They all include the same scripts and components. Is there any way to convert all of them to prefab instances?

Comment
Add comment · Show 1
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 ShadoX · May 02, 2021 at 08:15 AM 0
Share

Not sure I understand the question correctly, but what about just dragging them from the Scene into the Project hierarchy like you would with any other Game Object that you want to turn into a Prefab ?

Also - if all of the GameObjects are the same, then why not just create 1 Single Prefab of 1 and then use it for those 100~200 objects in the scene ?

Or if that is what you are trying to achieve then and actually just replace all of those 100~200 objects in the Scene with a Prefab then I would probably suggest to create a Prefab and then write a script that just checks your entire scene for the objects you want to replace with the Prefab and have the script do it for you, by "replacing" the GameObjects in the Scene with an instance of the Prefab.

At least that's how I would go about it

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Long2904 · May 05, 2021 at 10:51 AM

So you want to convert some game objects in your scene into a prefab? Why? What did you mean when you said you had already configured those game objects? What kind of configuring did you apply to each of those objects (scaling, rotating, etc, or changing some properties of some components)? You should only use a prefab when you want to change an object in one place and then apply it to other objects. There's no built-in way for you to convert all those same objects to a prefab instance, you have to make a script that loops through all of your objects and replace it with your desired prefab (also remember to save the configuring you did on those objects). Here's an example:

 // This is the component that already on your objects that you configured on
 public class YourComponent : MonoBehaviour
 {
     public int data;
 }
 
 [ExecuteInEditMode] // This will make the script run in editor mode
 public class ConvertObjectsToPrefab : MonoBehaviour
 {
     public GameObject desiredPrefab; // you have to make your desired prefab first and then drag it in here
     public bool stop = false;
 
     private void Update()
     {
         if (!stop)
         {
             if (desiredPrefab != null)
             {
                 // This will find all of your objects with the type YourComponent. This is slow but idk if you have any other ways to find all the objects (does your object have a tag?)
                 // You can use GameObject.FindGameObjectsWithTag if your objects use the same tag
                 YourComponent[] allObjects = FindObjectsOfType<YourComponent>();
 
                 for (int i = 0; i < allObjects.Length; i++)
                 {
                     GameObject instance = Instantiate(desiredPrefab, allObjects[i].transform.position, allObjects[i].transform.rotation);
 
                     // This is for saving the changes that you make on each objects
                     instance.GetComponent<YourComponent>().data = allObjects[i].data; // if your objects have other scripts and data that you want to save then just do the same
 
                     Destroy(allObjects[i].gameObject);
                 }
                 stop = true; // prevent the code to run multiple times
             } 
         }
     }
 }

Put this script on a game object, then assign the prefab that you want to replace your objects with. It will automatically run. After that, you can just remove the script.

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
avatar image
2

Answer by kevinhart1001001 · May 05, 2021 at 10:06 AM

Yes - just take the object you want to make a prefab, and drag it from the hierachy into the Project folder - a new Asset should appear and you can drag it into your game :)

Comment
Add comment · Show 2 · 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 regin · Jun 04 at 07:31 PM 0
Share

This is the better answer. Thanks.

avatar image Long2904 regin · Jun 05 at 03:06 PM 0
Share

Maybe you misunderstood the question. The OP already had 100-200 objects that were on the scene. Drag an object to the project view only make that object into a prefab. Those 100-200 objects are still just ordinary objects in the hierarchy. Not to mention that those 100-200 has slightly different values and stuff.

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

159 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Update prefab instance with local values (Editor Script) 0 Answers

Making multiple instances of a prefab,Instantiating multiple instances of a prefab 1 Answer

Prefab trows Error: 0 Answers

CompareTag to multiple GameObjects 1 Answer

Change image visibility of newly instantiated prefab? 1 Answer


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