- Home /
Can you revert a prefab in-game?
I have my character set up as a prefab, and right now, I am re-instantiating it every time you die (because I'm changing a lot of things inside the character upon death). But this is leading to a lot of problems with lost connections that I have to repair by re-finding the character object in scripts that need to access it. This could all be made much simpler by a function like RevertPrefabInstance, but that works in-game, rather than just in the editor. Does this exist?
Not as such, but you could wire up a way to store the player's prefab in the player class, and in OnEnable, read the data back in from the prefab.
I don't understand what you mean by revert it? Just revert it back to the default prefab settings, before anything has potentially been changed? If that is what you mean, I don't understand how that would be any different from just instantiating a new one.
I have it instantiating a new object now, but it's the player object, so other scripts need to access it, like the camera to follow it. This means that whenever a new one is created, those scripts have to re-find the player object, searching through all of the objects in the scene, just to link the player object back up. Loius, I'm not sure exactly what you're suggesting, but this isn't just once the component is enabled, it's at any point in the game after the player dies, to put all of the pieces back in place. But it's way too complex a character to write up a script to reassemble it. I just want to not have to destroy the game object, and ins$$anonymous$$d replace it with a fresh prefab.
You are probably better off using a Singleton pattern on your player - then nothing has to search to find it. They can do Player.instance and there it is. What language do you use?
Singleton Pattern:
Declare a static variable of the type of script, call it something sensible like instance or current
Set it to this during Awake()
Clear it during OnDestroy if it's still set to this
The fundamental problem here, I think, is that you don't Instantiate in video games.
These days I mainly live to promote my answer regarding pools so here's the link ..
http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
Also regarding prefabs. I feel it's well worth pointing out: there is absolutely no reason you have to use prefabs. For example FWIW in my co. we just don't like them and never use them for any reason, anywhere. So just don't panic about prefabs.
Answer by SubatomicHero · May 03, 2013 at 08:28 AM
You could always enable or disable the gameobject rather than destroying and re-instantiating the object, it's what I do in my projects.
i.e once an object is instantiated instead of destroy you can use:
Gameobject.SetActive = false;
then respawning is true
Answer by afra-frantic · Aug 12, 2017 at 06:38 AM
I think this link will be useful: https://docs.unity3d.com/ScriptReference/PrefabUtility.ResetToPrefabState.html
It's not - PrefabUtility is part of the UnityEditor namespace - the OP specifically states they want to do this in-game.
Your answer
Follow this Question
Related Questions
Why prefab revert does not work for position and rotation? 1 Answer
Reverting an object with children to its original prefab-state on runtime. 0 Answers
How do I reset a script's values at run time to what they were at start? 3 Answers
Why unity applies prefab position to my reverted gameobject ? 0 Answers
Reverting to prefab-state several game objects at once with stock Unity 3 Answers