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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to access objects from another scene in edit mode

Discussion in 'Scripting' started by Mehran_Akhavan, Aug 29, 2023.

  1. Mehran_Akhavan

    Mehran_Akhavan

    Joined:
    Sep 25, 2021
    Posts:
    7
    I'm working on a metroidvania style game. The game has many rooms with multiple entry points which player can move between them. Each room is a scene in Unity. For changing scenes correctly, I have an "EntryPoint" object which when player collides with it, the game reads its data and loads the correct scene and puts the player in corresponding EntryPoint in the new scene. So I have many EntryPoints which point to each other and they are symmetric (Meaning if Entrypoint1 points to EntryPoint2, EntryPoint2 must point to EntryPoint1).
    When I'm designing the levels I need to be careful to set EntryPoints correctly and need to change scenes a lot to set them right. I want to create a CustomPropertyDrawer for my EntryPoint objects to ease the process. Something like selecting target scene in it and it will list all EntryPoints in that scene which I can select. Here I need to read objects from another scene. Ideally if I select an EntryPoint, the EntryPoint in the other scene should change to point to the EntryPoint I'm editing. I need to write to an object in another scene to this purpose.
    Is there a way to achieve this?
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    If I understand you correctly, you're basically asking how "portals" or even different doors work between scenes?

    I'm thinking that all of this should be saved within your scene to scene transfer of data, and your technical spawn point should just be changed during that new scene load. As for example if the next scene is basically a default, you always spawn at (0,0,0) and start that map from there. However if you chose a special teleport or door, then when that scene loads you get set to that location (50,15,100).

    Or am I misunderstanding you?
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    One limitation of Unity is that you can't serialise references to objects across scenes. The way I've handled this is via scriptable objects, where a pair of scriptable objects can reference each other, making a 'transition'. Naturally they don't live in scenes, so a container component at each end of the transition - referencing one of each of the SO's - can handle moving the player from point A to point B.

    The main purpose for linking these two SO's together is so one can tell the other, "Hey, you're the transition point the player moves to when the scene loads!".

    The sequence is roughly:
    1. Play activates transition
    2. Transition object in current scene tells its paired object that it's the next entry point
    3. Unload current scene (this is assuming additive scene loading) (fade to black)
    4. Load next scene
    5. Transition point component in loading scene sees it's the 'active' transition point, and moves player to itself
    6. Un-fade screen
     
    Mehran_Akhavan and CodeRonnie like this.
  4. Mehran_Akhavan

    Mehran_Akhavan

    Joined:
    Sep 25, 2021
    Posts:
    7
    As you guys said I think my best bet is to use file/scriptable objects and write a custom tool to edit EntryPoints and EntryPoints load their necessary data on scene load.
    Thanks for your answers