Search Unity

Reset prefab instance to default

Discussion in 'Scripting' started by Ceffa93, Oct 1, 2014.

  1. Ceffa93

    Ceffa93

    Joined:
    Dec 2, 2013
    Posts:
    46
    Hi everyone,
    in my game i have around 20 prefabs (with children), and i instantiante all of them before starting to avoid instantiation time.
    Sometime, during the game, i ghave to take one of them and put it back like it was at the start of the game. But this means that i have to reset all of the children positions, remove the scripts i temporarly attached to the children and something else...is there a better way? What i'm currently doing is ugly looking (a tons of resets) and i think even not so optimal
     
  2. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    When making prefabs it's usually best to make them so independent that you can just drag and drop them to scene without problems.
    Secondly it's good to keep the GameObject hierarchy so that the components in the children are mainly there to just notify and listen the commands from the pårent object. This way it'll be easy to reset stuff like animation, colliders or the visual re-presentation of the object e.g model.

    This way the main component can save the relevant values on start and reset to them when needed, as well as tell all the child components to go back to their default state if necessary.

    In one of my projects I have serialized Level data filled with Entities that I spawn to the scene trough a separate script. The behaviors have an reference to this object which they can use to reset their values to default.
     
    Last edited: Oct 2, 2014