- Home /
How to save a gameobject with script generated mesh?
hi, I have created a sphere by changing meshes of a cube, and I would like to save it as a prefab using this code : GameObject Geo; Geo = new GameObject("myWorld"); MeshFilter GeoMesh = Geo.AddComponent<MeshFilter>(); GeoMesh.mesh = local; GeoMesh.sharedMesh = local; Geo.AddComponent<MeshCollider>(); Geo.AddComponent<MeshRenderer>(); PrefabUtility.CreatePrefab("Assets/World/myWorld.prefab", Geo); AssetDatabase.SaveAssets();
I only save the empty game object, without the mesh. I tried different things but I was never able to save the mesh with the game object.
Hi, the code is correct but the mesh need to be created in an Asset saved on disc and referenced by the prefab(s). To do this, use AssetDatabase.CreateAsset( my$$anonymous$$esh, myPath ) before creating the prefab. (sorry, I just have time for a quick comment right now)
Answer by webcam · Jun 16, 2017 at 04:01 PM
You just need to create a prefab set up script with a reference to an empty prefab called MyWorld
public GameObject myPrefab;
//drag your prefab from the project view into here
//put the rest in start and run once
MeshFilter GeoMesh = myPrfab.AddComponent<MeshFilter>();
GeoMesh.mesh = local;
GeoMesh.sharedMesh = local; Geo.AddComponent<MeshCollider>();
myPrfab.AddComponent<MeshRenderer>();
PrefabUtility.CreatePrefab("Assets/World/myWorld.prefab", Geo);
AssetDatabase.SaveAssets();
Your answer

Follow this Question
Related Questions
Using XML files and playerprefs together 0 Answers
How can you save and update data between scenes and upon log in? 1 Answer
Google Cloud Save Data returns empty. 1 Answer
Save Datasets at runtime 2 Answers
Threads? Or just Coroutines? 0 Answers