Great question!
Can you edit a variable on an object in a scene not currently loaded/opened?
Yes, but you cannot directly edit scene objects that aren't loaded in the editor. Unity's inspector and typical runtime/editor references operate on loaded scene objects. However, you can still modify objects in scenes that are not currently opened in the editor by:
- Loading the scene additively in the editor, modifying the object, then saving the scene.
- Or using the Prefab system, or other asset-based editing techniques.
- Or using serialized representations like
SerializedObject on scene assets or Serialized files.
Details and common approaches:
1. Loading the Scene Additively, Modify, and Save
You can programmatically open a scene additively in the editor (not switching away from your current scene view), access objects, modify variables, then save the scene back. This works because the scene is loaded into memory, letting you modify objects as if the scene was opened.
Example workflow:
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
public class SceneObjectModifier
{
public static void ModifyObjectInScene(string scenePath, string objectName)
{
// Open the scene additively (in the editor, does not unload current scene)
Scene scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
if (!scene.isLoaded)
{
Debug.LogError("Failed to load scene");
return;
}
// Find the GameObject by name inside the scene
foreach (GameObject rootObj in scene.GetRootGameObjects())
{
// Recursive search or find the target GameObject
GameObject targetObj = FindInChildren(rootObj, objectName);
if (targetObj != null)
{
var component = targetObj.GetComponent<MyComponent>();
if (component != null)
{
component.myVariable = 123; // modify your variable
EditorSceneManager.MarkSceneDirty(scene); // Mark the scene as dirty so it can be saved
// Save changes back to scene asset
EditorSceneManager.SaveScene(scene);
}
}
}
// Optionally unload the additive scene so editor state isn't cluttered
EditorSceneManager.CloseScene(scene, true);
}
// Recursive search helper
private static GameObject FindInChildren(GameObject parent, string name)
{
if (parent.name == name) return parent;
foreach (Transform t in parent.transform)
{
GameObject found = FindInChildren(t.gameObject, name);
if (found != null) return found;
}
return null;
}
}
2. You Cannot Modify Scene Objects When Scene Is Completely Unloaded
If a scene is not loaded in the Editor at all, its GameObjects are not instantiated in memory, so you can't directly modify their components or variables in code. You must load it (additively or single) first.
3. Prefab and ScriptableObject Editing
If your objects are prefabs or ScriptableObjects, they can be edited directly as assets without loading a scene.
Summary:
| Scenario | Can you directly edit variables without loading the scene? |
|---|
| Scene currently open in editor | Yes |
| Scene loaded additively | Yes |
| Scene not loaded (unloaded) | No, need to load first |
| Prefab or ScriptableObject asset | Yes |
If you want to modify objects inside scenes you don't want to load visually or affect your current editing session, loading the scene additively is the best way to go.
Let me know if you want a full example script or further guidance on this!