Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Creative_calibur · Feb 12, 2019 at 12:03 PM · scripting probleminstantiateprefabsgameobjectsdestroy object

Make a game object in a scene into a prefab with script

Anything I try to find on this is obsolete(from 2009,2010, etc.), basically, I have a game object already in the scene for a 2-D text based game. currently, I am trying to convert the game object in the scene to a prefab while in play mode((Basically keeping the current transform(s) and all actives(true, and false) of said game object)example lets say I click a button that button runs a command to convert the current game object and all its children into a prefab) I am no good with custom editors and cannot find anything under using unity; am I overlooking something or can you not make a game object into a prefab while running

what I'm trying to do in a sense

public GameObject = ExampleObject; public GameObject = ExamplePrefab;

convert ExampleObject to Prefab, set example prefab to equal prefab(i know how to do that last part)

then ill put an OnClick for destroying the game object and instantiate the prefab

this is a very rough example and not to the entirety but I hope it was a decent enough example.

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 alikanat · Feb 12, 2019 at 12:27 PM 0
Share

As far as i know this is only possible in Editor so you can not do this while running your game. What you can do is create the game object while running and not destroy it when the next scene is loaded.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Deathdefy · Feb 12, 2019 at 07:25 PM

Once you enter playmode there is no longer the concept of a "Prefab". Once everything is compiled they are compiled down to objects. If you want to instantiate a new version of that same object, you can just store off that object into a GameObject in your script and instantiate that.

 private void Test()
 {
      GameObject objToSpawn = objectIWantToCopy;

      GameObject newInstanceofSpawn = Instantiate(objToSpawn);

      Destroy(objectIWantToCopy);
 }


That would do what you want only at runtime. Once you uncheck play, this gets destroyed because it was created at runtime. You could look into ExecuteInEditMode as well.

https://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html

This allows your script to be executed while you are in edit mode. Then you could look into using the editor tools for creating prefabs.

 PrefabUtility.CreatePrefab(prefabPath, newInstanceOfSpawn);

I would 100% say your best shot at doing this is creating an editor script that will convert these objects for you. I know you said you aren't that great with them but the only way to get better is to practice creating them :).

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

193 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 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

Script inside instantiated prefab run lag behind 1 Answer

Tracking instantiated objects 1 Answer

Instatiate to the next scene prefabs from an array 1 Answer

Wrong coordinates of setting position of the object 1 Answer

Instantiating Multiple Prefabs at Runtime 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