Saving selfcreated Object permanently as Asset?

Hi,

i am trying to save a GameObject/Prefab which was created on runtime to my project assets, unfortunately when i call AssetDatabase.CreateAsset () the prefab is created in my project folder - BUT it’s spawned so i have two objects in the scene (one which i used to call AssetDatabase.CreateAsset () and the other one which is named after the prefab just created) this second object won’t disappear after i leave playmode and i get an error: “Some objects were not cleaned up when closing the scene”:neutral:
Furthermore after leaving playmode the prefab in my project folder becomes empty only the transform component remains:shock:
I’m looking for a way to store a GameObject permanently so i can create it once and then instantiate it in further game sessions…

Thanks for your support,

BPR

using UnityEngine;
using System.Collections;
using UnityEditor;

public class AssetCreation : MonoBehaviour {
	
	Texture2D pico;
	GameObject oob;
	public Texture2D display;
	// Use this for initialization
	void Start () {
		pico = new Texture2D(display.width,display.height);
		
		for (int i =0; i < display.width; i++){
			for (int j = 0; j < display.height; j++){
			pico.SetPixel(i,j,new Color(1f,0.0f,0.0f,1f));	
			}
		}
		pico.Apply();
		
		
		
		oob = new GameObject();
		oob.AddComponent("MeshFilter");
		MeshFilter filler =(MeshFilter) oob.GetComponent(typeof(MeshFilter)) ;
		filler.mesh = new Mesh();
		Vector3 [] vert = {new Vector3(1f,0f,0f),new Vector3(1f,0f,1f), new Vector3(0f,0f,0f)};
		int [] tries = {0,1,2};
		Vector2 [] uvs = {new Vector2(1f,0f),new Vector2(1f,1f),new Vector2(0f,0f)};
		
		filler.mesh.vertices = vert;
		filler.mesh.triangles = tries;
		filler.mesh.uv =uvs;
		
		filler.mesh.RecalculateNormals();
		
		
		oob.AddComponent("MeshRenderer");
		MeshRenderer rend = (MeshRenderer) oob.GetComponent(typeof(MeshRenderer));
		
		rend.material.mainTexture = null;
		rend.material.mainTexture = pico;
		
		
		
		AssetDatabase.CreateAsset (oob, "Assets/test.prefab");
		AssetDatabase.SaveAssets(); 
		
	
	}

}

for that u need to write editor scripts .
if you are not dealing with complex things.u can just drag and drop the gameObject to the asset floder at runtime ,it will create a prefab of that particular state of the gameObject .

Hi, thanks i will try it this way but drag and drop is no option since in the game this is supposed to happen automaticly :wink:

The PrefabUtility class might be of use here (possibly followed by an AssetDatabase.Refresh).