- Home /
Creating prefab from instance of a GameObject loses materials
Hi everyone!
I'm currently working on a project where we build a large world made of several tiles that I need to export in order to reimport them later on several distant devices. Each tile is made of a GameObject having a mesh renderer attached to, containing the models of the buildings, and a child object containing the mesh of the ground.
These tiles are created through a script I made, which imports the model (as .DAE files) and instantiates a GameObject on my scene with all the corresponding materials/textures (specified in the .DAE). The corresponding line on my script is:
var go : GameObject = Instantiate(Resources.Load(meshName));
then this gameobject is the parent object and automatically has the ground mesh attached to.
Here is the list of the materials attached to the instantiated scene object I want to export:
Now that my tiles are correctly created, I need to export them so I wrote a script that generates a prefab for each parent GameObject:
function generatePrefab (initialGO : GameObject){
//Generate the name of the prefab
var path : String ="Assets/Prefabs/" + initialGO.name + ".prefab";
//Create the prefab
var prefab = PrefabUtility.CreatePrefab(path, initialGO, ReplacePrefabOptions.ReplaceNameBased);
}
The issue is that my prefab has the correct objects attached to with the correct hierarchy and LOD group, but all the materials are missing as you can see in the picture below:
I tried to store the materials array of each renderer and reassign it to the prefab's renderers but it doesn't change anything. I also tried all the CreatePrefabOptions but I couldn't see any change. I suppose the issue comes from the instantiation of the gameobject (so the materials by the way) but I couldn't find anything on the web and I can't see another way of doing it.
I hope that the English mistakes I made won't hurt anyone :)
Thanks in advance
Gunt
Your answer
Follow this Question
Related Questions
Use assets from another project? 1 Answer
How to add an asset to a script-enabled public game object? 1 Answer
Is there a way to print Unity objects using 3D printer? 2 Answers
Programmatically Exporting an Asset Package 4 Answers
How can you export a prefab as a package and retain a Lightmap (that was generated by Unity)? 5 Answers