Search Unity

Assigning a prefab's child to another prefab's inspector

Discussion in 'Prefabs' started by Epimolophant, Apr 19, 2020.

  1. Epimolophant

    Epimolophant

    Joined:
    Dec 6, 2014
    Posts:
    33
    I'm working on a game with levels, which contain doors that lead to other levels (each level is a prefab). Entering a door causes you to move to another prefab, on a specific spawn point.

    My solution was to create spawn objects, and then I can associate each spawn to its equivalent door script on the inspector.

    The problem is that I cannot open two prefabs at once, so it becomes impossible to associate the target spawn from another prefab to the door script inside the currently open prefab.

    Is there any way to make this work? Or maybe a better approach to the problem? Thanks.
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    I'm unclear on what you're trying to achieve. From what you describe it sounds like you want to assign a child object in one Prefab Asset (not instance) to a script in another Prefab Asset (not instance). How does that help with creating a door in one level that references a spawn point in another level?

    If you do need an Asset that references a child in another Asset, then yes, that has never been easy in Unity (level 1 children were easy before Unity 2018.3 but not deeper children). One workaround is to have a script on the root of the Prefab whose child you want to reference and that script has a field with a reference to the child. Then the other Prefab can reference that script and get the child from there.
     
    Gomphosus likes this.