- Home /
Setting attribute in prefab
I have an object referencing a prefab which was setted by drag'n drop at "public GameObject gameObjectRef" When I set an attribute on gameObjectRef, it alters the prefab directly and the change is keeped even after stopping the game and starting it again. Is it possible to set this variable without alter the prefab object?
So you're asking "How can I edit a prefab without editing it"? This is kind of unusual. Can you explain more what exactly you're trying to do? I think it's basically possible but I don't want to suggest something too complicated if you're not actually trying to do that. Why not create an instance of the prefab with Object.Instantiate and then set the attribute on that?
Well, yes, if I edit variables through inspector in game mode, those variables are not kept, right? I want to keep the prefabs as instance of objects, and change only them without changing the "class". Right now I'm thinking on instantiating the prefab and put it on position (-999,-999,-999) just to have a object that I can set variables.
Actually no, if you select a prefab in the inpector and edit the variables on it during game mode, those changes ARE kept too. They aren't kept if you edit them on an instance of the prefab. The usual thing to do is to instantiate the prefab (if the position doesn't matter then you don't even need to bother putting it at any position) and only use and change that instance while the game is running.
Also, I've found it useful to have a convention where I put "prefab" in the variable name of anything that's supposed to be a prefab dragged in in the inspector, so if I see something called gameObjectPrefab (for example) I know that the only thing I should ever do with it is call Instantiate on it, not change its attributes.
yeah, that's what I'm doing, but it feels not performatic. how expensive is to instantiate a prefab? will it consume vram (using a vertex buffer for the renderer and etc) or just the normal memory to keep the object data?
It's expensive if you're instantiating it every frame, but if you just do it once when your game or level starts up then it's not that bad. And things that consume VRA$$anonymous$$ are shared assets which get loaded if your prefab is even referenced by the scene, and they won't get loaded twice (and a prefab is rarely if ever rendered directly) so you don't need to worry about that. By the way, it is technically O$$anonymous$$ to modify prefabs while the game is running in a built player (a final build running on someone's computer can't possibly change the assets in your editor, after all), so you could put #if UNITY_EDITOR then change the prefab #else use an instance of the prefab, but it sounds like a premature optimization to me.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064607im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Is there a way to set components in a prefab before it is Instantiated? 0 Answers
How to associate prefab with a variable in C#? 1 Answer
ThreadCheck error when making Prefabs 1 Answer
How do I script a prefab like I would code a class? 1 Answer