- Home /
Prefab loses its materials after running the game - Why?
In my Resources folder I have a prefab. That prefab has a few sub-meshes to which I want to apply a material/texture. So I assign a material by clicking the little black triangle next to "Element 0" in the Materials list & choosing a material. Then I save, and hit the play button
and test my game. Everything is fine. But when I return to the project view in Unity, my meshes no longer have a material. Element 0 says, "Missing (Material)". If I run the game again at this point, those objects have no materials or textures.
The result is I have to re-apply the materials every time I run the game. Can someone tell me what I have to do to make those materials permanent, so they don't get removed when I test the game?
Visual Aids:
Before: 
After: 
Note: I have a script that changes the texture on these meshes during the game. The code that changes the texture is:
myObject.renderer.material.mainTexture = www.texture;
where "www" is a WWW object.
However, I wouldn't expect that to affect the project files or settings. Am I missing something?
Answer by Eric5h5 · Aug 31, 2010 at 07:46 PM
You should only change instances of prefabs, not prefabs themselves. I would generally recommend not using Resources.Load unless you really have to, because you're more likely to make mistakes like that. Typically you do something like this:
var go : GameObject;
function Start () { var instance = Instantiate(go); instance.renderer.material.mainTexture = whatever; }
So you're saying that it is in fact the script that is causing the material to disappear from the prefab?
If I need to dynamically create instances of the prefab at runtime, what better alternative is there to Resources.Load?
@duggulous: yes, it's the script. I edited my answer with some code.
yep, changing the script fixes it. In your example I'm still not sure how to assign "go" to the prefab without using Resources.Load though...
Your answer