Edit and save prefab with code

Hi !
i’m a beginner so my question is maybe…stupid :sweat_smile: : how do i edit and save my prefabs by using script ? I want to create a script that can edit and save my prefab but i dont know how to do ><.
I pretty sure that i need to use * Using UnityEditor * but i dont know more… i’m lost and i can’t find any tutos like i want.
So if u have any idea to help me it could be very usefull coz’ i dont know how to start :,(
thanks

Hi Maeve!

It’s not too hard to do. What you’re looking for is the PrefabUtility class. Specifically check out the PrefabUtility.SaveAsPrefabAsset.

There’s good sample code on that page. Looks something like this:

PrefabUtility.SaveAsPrefabAsset(gameObject, localPath, out prefabSuccess);

Good luck!

hi !
thx for the answer !!
i still have a problem : visual cant found “prefabUtility”

Maeve_, that isn't saying it cant find it. Yellow lines like that are some type of suggestion, not errors. What happends when you run this?

Also, you probably dont want to call the SaveAsPrefabAsset on every update, that will be more than necessary. You should have some kind of trigger. Perhaps only save on keydown like your're doing above.