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.

Question Change prefab on plane dynamically

Discussion in 'AR' started by rssamarakoon, Mar 30, 2023.

  1. rssamarakoon

    rssamarakoon

    Joined:
    Nov 29, 2021
    Posts:
    10
    Hello, I am creating an app with two scenes. The first scene has a list of items and second is an AR scene. On selecting an item in the list I want to load the second scene and place the corresponding 3d object. I am using AR Foundation Sample Script and able to place a prefab which is the same regardless of which item is selected in the previous scene. Please advise how to dynamically change the prefab, depending on the item selected in the previous scene. PS: I know a singleton can be used get it working, but trying to avoid it. Thanks.
     
  2. WyattBUnity

    WyattBUnity

    Unity Technologies

    Joined:
    Mar 27, 2023
    Posts:
    35
    I would recommend creating a list of the prefabs that you want to create, in some kind of script that can hold references to prefabs, and then store the reference once they select it (or store a number of the index relative to the pre-determined list if you like) and when you move scenes, you'll want to make use of Anchors to attach them to places on planes if need be, but the "same" end prefab will be one that finds the script/component that holds the selected prefab, and then instantiates said prefab into the new scene.
     
  3. rssamarakoon

    rssamarakoon

    Joined:
    Nov 29, 2021
    Posts:
    10
    @WyattBUnity Thank you for the reply. I have a SceneManager script for switching scenes. Would you know how to get a reference to XR Origin object( which Place on plane script is attached to) in Scene Manager script(or any script)? Once I get a reference I will be able to set the prefab on Place on plane script.
     
  4. WyattBUnity

    WyattBUnity

    Unity Technologies

    Joined:
    Mar 27, 2023
    Posts:
    35
    There may be a better way, but
    GetComponent<XROrigin>()
    will work, but be mindful that it would be bad practice to call this line every frame, so storing a reference after calling this line would be better.
     
    rssamarakoon and andyb-unity like this.