- Home /
How do I revert prefab instance properties to their defaults?
Hey,
I have an object pool manager class. It basically stores a bunch of prefabs (rockets, giblets, etc.) and manages instances of those prefabs to avoid the overhead of re-instantiating. Other functions can request instances, which follows this basic logic:
/*** THIS IS PSEUDOCODE - just for showing concepts. I know these won't actually work :) ***/
Dictionary <string, List <GameObject>> instanceDict; // Instances stored here
Dictionary <string, GameObject> prefabDict; // Prefabs stored here
// Spits out a requested instance
public GameObject CheckOut (string name)
{
if (instanceDict [name].Count <= 0)
instanceDict [name].Add ((GameObject) Instantiate (prefabDict [name]));
GameObject returnObject = instanceDict [name][0];
instanceDict [name].RemoveAt (0);
Activate (returnObject); // Real code uses SetActive (true), etc.
return returnObject;
}
// Places it back
public void CheckIn (GameObject obj)
{
string name = GetObjectName (obj);
instanceDict [name].Add (obj);
Deactivate (obj); // Real code uses SetActive (false), etc.
}
So the question is: How do I reset an already-existing instance\'s properties back to the prefab's, from within a running game? This is possible from within the editor, but to my knowledge there is no simple / actively encouraged way of doing this at runtime.
For instance: Say I have a "Robot Bee" prefab, which is, by default, a) in flight, b) not controlled by ragdoll physics and c) not dead. These are spawned from a hive and attack the player. However, once the player kills the bee, it will be dead, ragdolling, and not in flight for a bit before despawning automatically.
If that specific bee instance spawns again (which it probably will), it will be dead and ragdolling, unless I have a script specifically written to reset it.
I do not think this should be necessary, especially if one wants to have two varieties of bees controlled by slightly different prefabs ("warrior" bees, for instance, would fly faster, have more HP, and do more damage)
Is there a way to iterate through serialized properties of a prefab and one of its instances? I want to make as much of this game's logic manageable from inside the Unity Editor, and I am seeking a way to do this.
I know this is a tricky one - thanks in advance for the help.
Your answer
Follow this Question
Related Questions
How to add an asset to a script-enabled public game object? 1 Answer
GameObject change Position after game started 1 Answer
prefab is instantiating without a script 2 Answers
Is it possible to disable/enable game-object inside Instantiate PreFab c# 0 Answers
PlayerRespawn class wont Instantiate the player prefab 1 Answer