Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Reinitialize prefab?

Discussion in 'Prefabs' started by DKaras, Apr 12, 2021.

  1. DKaras

    DKaras

    Joined:
    Sep 5, 2015
    Posts:
    5
    Let's say that during the runtime, I have some gameobject that I want to reset to the state of its coresponding prefab (for example - to re-enable all colliders, unparent all other gameobjects that weren't there at the start etc.),

    The typical approach would be to destroy said gameobject with GameObject.Destroy(gameobject) and then recreating it with GameObject.Instantiate(prefab).

    The problem with this approach is the fact, that reference to said gameobject may be held by other objects or may exist in Zenject's context.

    Is there any straightforward way to do this or do I need to write some custom script that reverts all the unneeded changes?
     
  2. pietrodenicola

    pietrodenicola

    Unity Technologies

    Joined:
    Dec 8, 2020
    Posts:
    43
  3. DKaras

    DKaras

    Joined:
    Sep 5, 2015
    Posts:
    5
    Thank you for the reply, but isn't it only applicable in editor scripts? (judging by the namespace and example in docs)
    During regular runtime I get following error: Calling apply or revert methods on an object which is not part of a Prefab instance is not supported.
     
  4. pietrodenicola

    pietrodenicola

    Unity Technologies

    Joined:
    Dec 8, 2020
    Posts:
    43
    You are totally right. You can't use that function I suggested to revert to an initial state. I just asked a colleague who is more expert and apparently there is no other way than just writing custom script.
     
    DKaras likes this.