Search Unity

Reference original prefab on disk from instance of prefab

Discussion in 'Prefabs' started by Marble, Oct 14, 2019.

  1. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    I have a MonoBehaviour (MyComponent) on a prefab with a reference like so:

    public MyComponent myPrefab;

    If I drag the prefab itself from the project view into the inspector to initialize this reference, then when I instantiate the Game Object the "myPrefab" reference points to the new instance instead. This makes sense, but how can I force the reference to keep pointing at the uninstantiated prefab on disk?

    I want a component to keep track of its original prefab for comparison to other instantiated prefabs, but I can't easily do so when the reference changes after instantiation or scene serialization.
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    No it doesn't unless you assign the instantiated Prefab to the myPrefab variable.

    Assign the instantiated Prefab to a different variable, and the myPrefab field will keep referencing the Prefab Asset.
     
  3. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Thanks for the rapid response, Rune. Here's a video of me attempting to do this and seeing the reference reassigned. At 0:20 I instantiate the prefab and see the reference change (which I've come to expect as behavior). At 0:32 I reload the scene and see the reference change (which surprised me). I wanted to preserve the reference to the original prefab in the project because equality comparisons of "myPrefab" between these prefabs fail after the "change" but not before.

    If this isn't the way it should work, should I file a bug report?
     
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Ah okay, it was not clear to me that you had the script on a Prefab and referenced the same Prefab in the script, e.g. that the Prefab references an object inside itself. In that case, the behavior you see is by design.

    When you need to reference the original Prefab, is that solely for editor tooling purposes, or do you need this functionality also in Play Mode and in built players? If it's for editor purposes you can use this API to find the corresponding object in a Prefab Asset, given an object in a Prefab instance:
    https://docs.unity3d.com/ScriptReference/PrefabUtility.GetCorrespondingObjectFromSource.html

    If it's for runtime use, you might have to go some other route, like referencing a ScriptableObject which references the Prefab, so it's not a direct reference from a Prefab Asset to itself. There's no way to force internal references to keep pointing to the Asset. They are patched up at a low level to keep being internal references, also after instantiation.
     
    AlonMixed likes this.
  5. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Yep - I can identify the prefab at runtime with an id or something. I just wanted to check if there was an easier way. Thanks for answering the question so clearly!
     
    Last edited: Oct 16, 2019