- Home /
Assign a mesh and material to an empty gameobject (JS)
Hi there,
Having a bit of a problem with assigning a mesh and material to an empty game object.
Im having prefabs in a resource folder. Reason for this is because I have to load prefabs based on a name which is a string which is possible when using resource prefabs with: Resources.Load("String", GameObject);
Now I want to assign the mesh and material from the loaded prefab to an empty game object. But when I do that, the material get lost from the prefab as soon as I runned the game once.
I use this script to assign them to the empty gameobject:
//Set the mesh
chestObject.GetComponent(MeshFilter).mesh = prefab1.GetComponent(MeshFilter).mesh;
//Set the material
chestObject.renderer.material = prefab1.renderer.material;
//prefab1 is the loaded resource prefab
//chestObject is the empty gameobject
Can somebody help me with this one? I don't want the resource prefab to lose it's material...
Comment