Access Inactive Objects Across Scenes

Hello everyone!

I recently discovered it’s impossible to define a variable using [SerializeField] (meaning you cannot drag-and-drop an object or a component into a field in the inspector), if said object is stored in another scene. The alternative I found was using ‘Find’, but only if the object in question isn’t inactive at start, otherwise it would not be able to access it. For example:

In this case, ‘Example Object’ cannot be accessed by this line of code:

exampleObject = GameObject.Find("Example Object");

Hierarchy

A walkaround I decided to use for now is creating an empty object with a script that acts as a database.
The script is in the same scene as the object I want to access, so I can simply drag and drop any item in the scene even if they are inactive, then make them public to be available from any scene.

Serialize Field Database

It’s a list, so I can access the item using its index like so:

GameObject firstGameObject = SerializeFieldDatabase.gameObjects[0];

Is this good practice? Is there a more efficient solution?
Thanks!

I have a resource that describes the standard ways to make references:
https://unity.huh.how/references

Which contains a page on cross-scene references:
https://unity.huh.how/references/cross-scene-references

Declaring raw static variables to do it is pretty non-standard, and you should instead use the singleton pattern. It’s much less work to maintain as you only have to declare the one static access variable, and the rest is normal. Using a list to prevent that maintenance is clunky and makes the code pretty unreadable.

FWIW methods like FindFirstObjectByType have an overload to include inactive objects: Unity - Scripting API: Object.FindFirstObjectByType

Even though it’s not best practice, if you do need it you should always use these methods over GameObject.Find.