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 /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Mar 24, 2019 at 10:39 AM · prefabsave datadeleted

How Do I Save An Instantiated Object?

I am trying to create a GameObject through a prefab, however when I end the game the object is deleted along with all of the information that it contained. I know this what is supposed to happen but I was wondering if there was any way to either save its information, stop it from deleting, or turn it into prefab.

here is my current code,

 using UnityEngine;
 using UnityEngine.UI;
 
 public class NewMaterialCreator : MonoBehaviour
 {
     public Button CreateMaterial;
     public Transform SpawnPosition;
     public GameObject MaterialCodePreset;
     public Text MaterialName;
    
     void OnEnable()
     {
         CreateMaterial.onClick.AddListener(MyFunction);
     }
     void MyFunction()
     {
          var NewMaterial = Instantiate(MaterialCodePreset, SpawnPosition.position, SpawnPosition.rotation);
         NewMaterial.name = MaterialName.text;
 
     }
 }



Thanks in advance for the help

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

1 Reply

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

Answer by dan_wipf · Mar 24, 2019 at 05:29 PM

have a look at the docs, how you save objects as prefabs

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

Comment
Add comment · Show 5 · 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 $$anonymous$$ · Mar 24, 2019 at 08:19 PM 0
Share

I am trouble having this work. Do I need to do anything else besides making the folder ("Editor"), and putting the script in it.

Also how do you select an object while the game is running.

avatar image dan_wipf · Mar 24, 2019 at 08:53 PM 0
Share
 var obj = new List<GameObject>();
   //obj.Add(instantiated Gameobjects)
 void OnDestroy(){
   for(int i = 0; i< obj.Count; i++){
     PrefabUtility.SaveAsPrefabAsset(obj[i], "Assets/$$anonymous$$yFolder/");
     }
 }
avatar image dan_wipf dan_wipf · Mar 24, 2019 at 09:02 PM 0
Share

something like this might helps. but i’m not on my computer right now so it’s not realy a optimized and relieable answer. just consider about override allready created asset at that path. have a look what options there are to save an asset in the assets folder

avatar image $$anonymous$$ · Mar 24, 2019 at 10:04 PM 0
Share

That Worked Great, but I want it to make new prefab every time I press a button.

At the current moment when I press the button it makes a prefab but the next time I press it nothing happens.

 public class New$$anonymous$$aterialCreator : $$anonymous$$onoBehaviour
 {
     public Button Create$$anonymous$$aterial;
     public Transform SpawnPosition;
     public GameObject $$anonymous$$aterialCodePreset;
     public Text $$anonymous$$aterialName;
    
     void OnEnable()
     {
         Create$$anonymous$$aterial.onClick.AddListener($$anonymous$$yFunction);
     }
     void $$anonymous$$yFunction()
     {
         var New$$anonymous$$aterial = Instantiate($$anonymous$$aterialCodePreset, SpawnPosition.position, SpawnPosition.rotation);
         New$$anonymous$$aterial.name = $$anonymous$$aterialName.text;
 
         var obj = new List<GameObject>();
         obj.Add(New$$anonymous$$aterial);
 
         for (int i = 0; i < obj.Count; i++)
         {
             PrefabUtility.SaveAsPrefabAsset(obj[i], "Assets/$$anonymous$$yFolder/$$anonymous$$aterialName.prefab");
         }
         
 
     }
 }

avatar image dan_wipf $$anonymous$$ · Mar 25, 2019 at 05:43 AM 0
Share

you have to put the var obj as a property, and add every instantiate new$$anonymous$$aterial to this list. on void OnDestroy() you loop trhough that list and add every object to your folder.


void OnDestroy is automatically called when you stop the game and return to the editor. so all the prefabs should be saved. to optimize, you might want to make an ifstatement if the object is allready in that folder =>


string localPath = "Assets/" + gameObject.name + ".prefab"; //Check if the Prefab and/or name already exists at the path if (AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject))) {


if true => PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab); }

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

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

Related Questions

FixedUpdate Crash when try to save prefab in runtime. 0 Answers

Saving Instantiated objects,Saving Instantiated objects 0 Answers

Prefabs and public variables 1 Answer

Prefabs getting deleted in all scenes! 0 Answers

Prefab with same scripts do the same thing 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