Search Unity

Question Swapping a part of a prefab with another prefab at runtime

Discussion in 'Getting Started' started by tleylan, Nov 8, 2022.

  1. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    I am doing something wrong but I would like to check if there is an established solution for this. I have a prefab that represents the player (in VR). It includes the camera rig, avatar and also the player UI menu (which can pop up). This was working for months but I was including every UI variation that could exist in any room and that won't scale.

    So I wanted to create separate UI prefabs for each world (that seems to have worked and to swap them into the Player UI node in my player. That is giving me grief along with an error "Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption".

    It suggests that I may be a) doing it completely wrong or b) targeting the wrong object. Is there an article or tutorial that shows how to swap prefab elements this way?

    Thanks.
     
  2. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    I believe I am a step ahead of where I was. I was referencing the prefab instead of an instantiated version of the prefab. Still an article outlining this would help.
     
  3. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    One or two steps closer. I can see the old UI menu is disabled and my new one is active in its place. Some complaint about the canvas (which I'm replacing also) when I try to delete the old menu.
     
  4. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    Got it... ta da... so nice to be able to monitor what is going on at runtime from within the editor!
     
  5. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    I jumped the gun. Slotted in the new prefab but there are some "wires" that need reconnecting. The camera for one and controller actions that make the hide/show the menu.

    So I'm back to asking if anyone knows of the method that professionals use to swap UI menus based upon the scene the player is in?

    I was going to change the target gameobject used to hold my menu by replacing the children inside of the one with the canvas. This would leave the "wiring" intact BUT then there is no canvas when I want to edit these menus and they are invisible in the editor. This just seems all like I'm approaching it wrong.