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 Reference to unloaded scene

Discussion in 'Editor & General Support' started by Vifi, Jul 26, 2022.

  1. Vifi

    Vifi

    Joined:
    Aug 5, 2013
    Posts:
    28
    Hi,
    i am writting this "favourites" tool in unity so user can pin their most used GameObjects and Assets from project to quick come back to them. For now i use Json and EditorPrefs to save references.
    One problem i stamble upon was that i am loosing references to GameObjects from scenes that are unloaded. Well, that make sens... but i wish to keep them somehow. They can be missing while scene is unloaded but i wish them to be set back when scene with that gameObjects is loaded agan. Do you have any idea how can i accomplish that?

    Thanks in advance and cheers!
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    3,515
    Unloaded scenes don't 'exist'. It helps to think of loading a scene as basically instantiating everything in the asset file, and unloading a scene as destroying everything in said scene. This is why you can load the same scene multiple times additively, and why cross-scene referencing is not supported.

    The only way I think this could be even remotely possible would be to serialise a pointer to something inside the scene asset's yaml, but even then I wouldn't know if it's possible to restore an asset reference with that information.

    Storing something within the scene makes this more possible, something you could use Hideflags with to maintain a hidden object in each scene that keeps tabs of these. Not sure how you would maintain this when multiple scenes are open at the same time, mind you, and I'm sure some would prefer addons not sliding hidden game objects into their projects.

    So yeah, I personally can't think of an elegant solution for this.
     
  3. Vifi

    Vifi

    Joined:
    Aug 5, 2013
    Posts:
    28
    Is there any way to make monobehaviour script to be on object only in the editor?
    I mean, i could just throw script onto gameobjects on different scenes and make them call to tool if enabled but i don't want to mess up scenes with that script and risk of forgetting to remove it before build so the ideal would be script that sits on gameobject but it isn't sent to build
     
    Last edited: Jul 28, 2022
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    3,515