Change material and animation from a prefab in runtime before instantiating.
My only doubt is accessing the prefab properties in resources, (if that's the case), so when i run the thing it spawns doing the animation i want as "layer default state" and the material selected visible.
I write this because i could not find anything else any other place. Hope someone can help me out. Second pics shows animator with different layers, read those layers can be moved via code so the first at the top is the one launching the default state. Haven't found it yet.
Answer by streeetwalker · Apr 09, 2020 at 06:29 PM
@punixer, do you have some overriding reason you cannot do all that after you instantiate?
Because if you edit the prefab at run time, to my knowledge, you will alter the prefab permanently - that is, any changes will stick with the prefab even after you stop running the game, and every instance of the prefab will take on the new properties you apply.
In other words, the only way to apply unique properties to instances of a prefab is to apply them to instances - and that should sound redundant! Any changes you apply you might as well apply in the editor.
Finally, public properties of script components attached to prefabs that reference other object aren't retained in prefabs anyway - you have to assign those at runtime after you instantiate.
Answer by punixer · Apr 09, 2020 at 07:19 PM
Hi @streeetwalker and thanks for the fast reply. I'm quite noob at coding, so i dont understand override too much. I make many instances for the prefab.
The option i'm doing is put a small script on the prefab checking the currentMaterial of another static script in which i loaded the materials dragging them and then assign through that script to the current prefab before instantiating. So instead of instantiate -> set material. (which consumes crazy memory), i just set it in the prefab. Maybe both are the same efficient-wise. No idea. And i guess i'll try the same with runanimatorcontroller for the animations. Thanks again.
Your answer
Follow this Question
Related Questions
my instantiation /prefab of dan wont play death animations like orignal dan 0 Answers
How to fix broken asset thumbnails? 2 Answers
Why have certain parts of my project gone blank? 0 Answers
Is it better to have several inactive prefabs in a scene, or one prefab that changes accordingly? 0 Answers
Assigning SBAR material to a Prefab 0 Answers