Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 CookieKirby · Jan 15, 2014 at 08:21 PM · gameobjectprefabchildcreateruntime-generation

How could I create a prefab from an exsisting gameobject at runtime?

I need to create a prefab from a gameobject at runtime, but without changing the gameobject. I also need this prefab to contain all children and grandchildren, unless the current prefab system already does this.

Comment
Add comment · Show 6
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 DaveA · Jan 15, 2014 at 08:25 PM 0
Share

Instantiating a prefab is exactly this, as long as all chidren etc are part of the prefab. If not, you want to make a copy of object(s)?

avatar image gfoot · Jan 15, 2014 at 08:36 PM 1
Share

What are you going to do with this prefab after you create it? If all you want to do is create copies of an existing GameObject subtree at runtime then you can easily do that using Instantiate - just pass it an object from the scene ins$$anonymous$$d of passing it something from a prefab.

avatar image CookieKirby · Jan 15, 2014 at 10:13 PM 0
Share

I need a prefab, I can't use instantiate though, because I am changing the original gameobject changes, but I'd like to continue to refer to the starting state of the gameobject.

avatar image robertbu · Jan 15, 2014 at 10:17 PM 0
Share

You can make copies of copies plus you can make changes after Instantiate() to the copies.

avatar image robertbu · Jan 15, 2014 at 10:44 PM 1
Share

I'm fairly sure you cannot create a real prefab at runtime. At runtime there is no folder structure to put files (they've been rolled into a single-file database), and all the Edit-time support for modifying asset database is missing from the Runtime.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by gfoot · Jan 15, 2014 at 11:22 PM

As others have said, I don't think it can be done exactly the way you want. I know you said you weren't interested in workarounds, but the best solution I have is making a clone of the source GameObject and parenting this snapshot under a disabled GameObject, so that it doesn't update or render. Then you can clone this elsewhere in the scene just as you would a prefab.

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
0

Answer by rutter · Jan 15, 2014 at 11:23 PM

There is no way to create prefabs in a built player. You can do it in the editor, but that doesn't seem to be what you're looking for.

I need to create a prefab from a gameobject at runtime, but without changing the gameobject.

You can certainly use Instantiate() to duplicate objects. You could disable those objects so that their state never changes. You could keep them between scenes with Object.DontDestroyOnLoad. You could use some static references to keep a "singleton" reference. In practice, that's the closest you can get to a "runtime prefab".

I also need this prefab to contain all children and grandchildren, unless the current prefab system already does this.

Prefabs contain one GameObject and all of its children (with the minor caveat that its children cannot be "nested" prefabs).

If you Instantiate() an object, the clone will contain copied children and components.

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

21 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

Related Questions

pooled prefab missing child 1 Answer

How to create and save a gameobject to a prefab with a script 3 Answers

Make a simple tree 1 Answer

[C#] An error occurs when trying to set a child to my GameObject in script. Why? 0 Answers

Instantiate prefab with different values each time 4 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