- Home /
How to save prefab's color/texture ?
Hi guys, Here's my issue : In my first scene I have a sphere, through a dropdown I'm able to change both color and texture. Then when I click on "Save" my sphere is saved into a prefab, so far so good ... In my second scene I would like to instanciate my sphere (with color and texture that I chose) by clicking a button. It works well but my sphere is all in fluorescent pink, what does it means ? No renderer ? camera trouble ?
When I'm saving my sphere (gameobject) into a prefab I guess I should specify to save color and texture, right ? How Can I do that ?
If anyone could help that would be a lot of help :D, thanks !
I'm doing something like :
Object prefab = EditorUtility.CreateEmptyPrefab("Assets/Resources/mySphere.prefab");
EditorUtility.ReplacePrefab(Sphere, prefab, ReplacePrefabOptions.ReplaceNameBased);
A = Resources.Load("mySphere") as Gameobject;
Gameobject go = Instanciate (A) as GameObject;
When you changing the object color or texture or anything for the material through the code, the Unity will copy out another copy of the material and save it to temporarily memory and the material will not store in project folder, then the material will get destroy after you close the game or editor.
Then when you call the save/replace prefab function to save the object to prefab, because the material is in temporarily memory so it wont save together with the object, that why when instantiate the prefab you will get fluorescent pink color because the material is not save in the project folder so it couldn't find the reference material for it.
A workaround is you save the color and texture data for the object, then you replace the color or the texture for the object everytime you spawn the object
Thanks alot for your explanations @walaoRI$$anonymous$$I ! That helps me to have a better understanding about all of this. I solved my problem, I chose to clone my prefab ins$$anonymous$$d of save it and then try to spawn it with parameters ...
Cheers ! :)
welcome, I corrected the answer a bit, I was type in rush juz now coz my boss looking for me lol...
Answer by HawkSandwich · Jun 16, 2017 at 09:45 AM
Heya :)
The pink you mentioned means there's no material. If you don't know, a material is what allows for textures to be displayed, maybe some colors, maybe a mix of the two depending on what shader the material is using.
So firstly, make sure there's a material on your sphere.
You can grab textures by doing "Texture someTexture;" "someTexture = someObject.GetComponent().material.mainTexture;"
As for color, you can do "Color someColor;" "someColor = someObject.GetComponent().material.color;"
Hope I helped!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How do I save a prefab? 1 Answer
Saving and loading Prefab clones at runtime, C# 2 Answers
Multiple enemy prefabs moving towards a single goal. Need help! 1 Answer