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
Answer by dan_wipf · Mar 24, 2019 at 05:29 PM
have a look at the docs, how you save objects as prefabs
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.
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/");
}
}
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
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");
}
}
}
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); }