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.

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.

Really that's fascinating. I always like the "Awake not called" nature of prefabs, but I guess you've got something different going on? The pooling covering it or something?

2 Answers

2

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

I think this link will be useful:

It's not - PrefabUtility is part of the UnityEditor namespace - the OP specifically states they want to do this in-game.