- Home /
How can I assign an existing material to a prefab?
Hello, I'm trying to apply an existing material to a prefab with this code:
myInstantiatedPrefab.renderer.material = Resources.Load("myMaterial") as Material;
But each time I try I get an error message saying:
There is no 'Renderer' attached to the "myPrefab" game object, but a script is trying to access it.
Yet the prefab does contains a mesh. So would anyone know how to assign a material to a prefab, please?
Answer by BiG · Nov 29, 2012 at 02:49 PM
That line of code seems right. Do you have a hierarchy, as your prefab? In that case, make sure that the mesh renderer is assigned at the top-level object, and not just at a child of it.
I tried adding a renderer component to the prefab itself but changing the material there has no effect on the meshes within the prefab.
So I ended up having to assign materials to the transforms inside the prefab. Doesn't seem very simple nor elegant to me, but I'm still a Unity beginner. Anyway, thanks for your help!
@Seith, did you ever get a more elegant solution? I'm having the same problem. Thanks!
Your answer
Follow this Question
Related Questions
Generated meshes/materials cannot be made into prefabs? 1 Answer
Setting sharedMaterial only setting for one object? 1 Answer
how do I assign a prefab's child to another prefab's script's public variable? 1 Answer
How can I set the material on an instance after creating it? 1 Answer
Variable not Assigned 1 Answer