Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiating a prefab and preserving contained reference to self

Discussion in 'Scripting' started by hedgehog90, Jun 12, 2017.

  1. hedgehog90

    hedgehog90

    Joined:
    May 18, 2016
    Posts:
    27
    This is a funny problem I've encountered.
    Here's the situation. Some of my prefabs contain components which reference the prefab asset itself that it belongs to or a sibling object from the prefab.
    Unity however has a policy of changing any self-referencing prefab object references to the newly instantiated version of itself.

    Confused? Me too.

    In some cases surely it should be allowed so a property contained within a prefab may reference the asset (or sub-asset) it belongs to and to retain this in an instantiated version.
    Should there not be a special attribute that Unity could take into account when serializing for weirdos like me?
    Something like [DontDuplicatePrefabSelfReferencesPleaseOKThanks]?

    I've read a few accounts of people running into the same problem as me, haven't found any solutions though.
    Is my whole approach wrong or am I simply too left-field and clever for Unity?

    It's a nightmare to google stuff like this btw. I've had this problem for weeks but it's only now that I can verbalize the nature of the problem precisely.
    Unity presents itself as simple, easy-to-use on the surface, but once you get right down into it makes you feel like Alice in Wonderland. Quirky as heck.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Personally have no issues with Unity. I'm more confused by what you want to do or what you are encountering. You're wanting to instantiate a prefab but have the newly created object point to the prefab instead of the newly created object?
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    You really don't want pointers to assets at runtime unless you absolutely need to. Changes made to them (in edit mode) are permanent, so you'll end up with your runtime game code editing your assets.

    What's your use case? You still have a reference to the original prefab, so if you really want parts of that, just copy them over after instantiation.
     
  4. hedgehog90

    hedgehog90

    Joined:
    May 18, 2016
    Posts:
    27
    I know it sounds odd, but to my mind that doesn't mean it should be dismissed.
    In my case, I'm trying to create a simple nestable prefab system (yes, I'm aware I can get these on the store), whereby instead of using Unity's own prefab system, I've got a component doing all the prefab and syncing for me, so I can nest prefabs without losing the link to the original prefab. The component in question needs to store a reference to the asset, not the newly created instance.
    TBH I have yet to encounter the problem as whenever I need to use this prefab component, it's on a non-prefab gameobject, but I figured there must be a scenario where this problem arises.
     
    Last edited: Jun 13, 2017
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Well, as @Baste said, your prefab is still there and you still have a reference to it. If you instantiate it but still need to link to something on the prefab, you just have to tie the new object into the stuff the prefab targets.