static void replaceInstances(SearchJob parentJob, SearchItem item, GameObject oldValue, GameObject newValue) { SearchItemSet searchSet = new SearchItemSet(); searchSet.OnDeserialization(); searchSet.AddNew(Keys.Global, false); SearchItemGlobal searchItem = (SearchItemGlobal)searchSet.items[0]; searchItem.SetType("Object"); DynamicTypeObject dto = (DynamicTypeObject)searchItem.GetDTDFor(typeof(UnityEngine.Object)); dto.SearchSubObjects = true; dto.SetObject(oldValue); ReplaceItemObject replaceItem = (ReplaceItemObject)dto.ReplaceItem; replaceItem.SetObject(newValue); SearchOptions options = new SearchOptions(); options.searchType = SearchType.SearchAndReplace; // does this matter anymore since asset scope is now essentially defined by what assets are passed in? SearchScopeData searchScope = new SearchScopeData(ProjectScope.SpecificLocation, AssetScope.Prefabs, new ObjectID(AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(parentJob.assetData.assetPath)), false, parentJob.scope.loadPrefab); SearchJob subJob = new SearchJob(searchSet, options, searchScope); SearchAssetData assetData = parentJob.assetData.Clone(); subJob.AddAsset(assetData); subJob.Execute(); // Now that we've executed the job we have to save a list of all objects from search results, because as soon as // we swap out the new object, the old object's position may shift in the hierarchy, making the PathInfo stale. SearchResultGroup group = subJob.resultSet.results[searchItem.sortIndex]; List <UnityEngine.Object> resultObjects = new List <Object>(); foreach (SearchResult result in group.results) { UnityEngine.Object resultObj = EditorUtility.InstanceIDToObject(result.pathInfo.objectID); resultObjects.Add(resultObj); } UnityEngine.Object.DestroyImmediate(oldValue); // now that we've deleted the object, let's rebuild the objects. for (int i = 0; i < resultObjects.Count; i++) { SearchResult result = group.results[i]; result.pathInfo = PathInfo.GetPathInfo(resultObjects[i], assetData); } parentJob.AddResultsFromSubsearch(item, subJob.resultSet.results[searchItem.sortIndex]); }