How can I instantiate a prefab/s in all loaded scenes or in selected scenes from list ?

What it does now is when I select gameobjects in a scene by clicking on the gameobjects and then clicking on the Replace button it will replace all selected gameobjects with prefab.

This is working fine for the current active scene.

For example I have two scenes in the Hierarhcy: Test1 and Test2
In scene Test1 I have a Camera and Directional Light.
In scene Test2 I have only Directional Light.

What I want to do is when I select the Camera in Test1 and click on Replace button that it will replace in Test1 the Camera with the prefab but also in scene Test2 add the new prefab in the same position rotation scale like there was a Camera.

The problem is also if I set the Test2 scene to be active but select the Camera in Test1 it will delete the Camera the in Test1 and will create the prefab in Test2 but I want to have the prefab created in both scenes no matter what scene is active.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PrefabReplace : EditorWindow
{
	[SerializeField] private GameObject prefab;
	public static List<GameObject> objectsSettings = new List<GameObject>();
	static int scenes = 0;
	static List<string> scenesNames = new List<string>();

	[MenuItem("Tools/Prefab Replace")]
	static void CreateReplaceWithPrefab()
	{
		EditorWindow.GetWindow<PrefabReplace>();

		scenes = SceneManager.sceneCount;
		for (int i = 0; i < scenes; i++)
		{
			scenesNames.Add(SceneManager.GetSceneAt(i).name);
		}
	}

	private void OnGUI()
	{
		prefab = (GameObject)EditorGUILayout.ObjectField("Prefab", prefab, typeof(GameObject), false);

		if (GUILayout.Button("Replace"))
		{
			objectsSettings = new List<GameObject>();
			var selection = Selection.gameObjects.ToList();

			if (prefab != null && selection.Count > 0)
			{
				for (var i = selection.Count - 1; i >= 0; --i)
				{
					var selected = selection*;*
  •  			var prefabType = PrefabUtility.GetPrefabType(prefab);*
    
  •  			GameObject newObject;*
    
  •  			if (prefabType == PrefabType.Prefab)*
    
  •  			{*
    
  •  				newObject = (GameObject)PrefabUtility.InstantiatePrefab(prefab);*
    
  •  			}*
    
  •  			else*
    
  •  			{*
    
  •  				newObject = Instantiate(prefab);*
    
  •  				newObject.name = prefab.name;*
    
  •  			}*
    
  •  			if (newObject == null)*
    
  •  			{*
    
  •  				Debug.LogError("Error instantiating prefab");*
    
  •  				break;*
    
  •  			}*
    
  •  			Undo.RegisterCreatedObjectUndo(newObject, "Replace With Prefabs");*
    
  •  			newObject.transform.parent = selected.transform.parent;*
    
  •  			newObject.transform.localPosition = selected.transform.localPosition;*
    
  •  			newObject.transform.localRotation = selected.transform.localRotation;*
    
  •  			newObject.transform.localScale = selected.transform.localScale;*
    
  •  			newObject.transform.SetSiblingIndex(selected.transform.GetSiblingIndex());*
    
  •  			Undo.DestroyObjectImmediate(selected);*
    
  •  			if (newObject != null)*
    
  •  				objectsSettings.Add(newObject);*
    
  •  		}*
    
  •  	}*
    
  •  }*
    
  •  GUI.enabled = false;*
    
  •  EditorGUILayout.LabelField("Selection count: " + Selection.objects.Length);*
    
  • }*

  • private void OnInspectorUpdate()*

  • {*

  •  Repaint();*
    
  • }*
    }

As far as i know you can’t change unactive scenes, you’ll have to set some bool to true and then when you load the other level, change the camera in that scene out for the prefab, if the bool is true.