PrefabUtility.RevertPrefabInstance() not working
So, I'm trying to make a code to grub the player in every scene and revert it to it's prefab values. Something must be wrong because it doesn't seem to be working. I'm using PrefabUtility.RevertPrefabInstance(player); where player is a gameObject.
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using System.Collections;
public class zCambiarTodasLasEscenas : ScriptableWizard
{
public Object[] scenes; //An array with al the scenes I want to modify
//creating a wizard menu
[MenuItem("Tools/CambiarTodasLasEscenas")]
static void CreateWizard()
{
ScriptableWizard.DisplayWizard("Edit Scenes", typeof(zCambiarTodasLasEscenas), "Run");
}
void OnWizardCreate()
{
//For each scene
foreach (Object s in scenes)
{
string path = AssetDatabase.GetAssetPath(s); //get the scene's path
EditorSceneManager.OpenScene(path); //open the scene
GameObject player = GameObject.FindGameObjectWithTag("Player");//find the player (is a prefab instance)
PrefabUtility.RevertPrefabInstance(player); /////////Revert the instance to the prefab values.
EditorApplication.SaveScene(path); //Save the scene.
}
}
}
Answer by dbiggsant · May 04, 2018 at 02:22 AM
If the prefab is disconnected from the original prefab, you may need to reconnect it first like this
PrefabUtility.ReconnectToLastPrefab(go);
PrefabUtility.RevertPrefabInstance(go);
Thanks! That was exactly it. I also created the object with this:
UnityEditor.PrefabUtility.InstantiatePrefab(object)
There's probly a bug with that that makes it lose the connection.
Answer by Diukrone · Jan 16, 2018 at 12:51 AM
Change this PrefabUtility.RevertPrefabInstance(player); TO THIS: PrefabUtility.RevertPrefabInstance(player go);
Please, Always consider checking Unity API Classes before start to develop a line,
Thanks a lot, I was stucked. I did check the API. Thing is it doesn't have examples so I was confused with what that "go" meant. I see now that it's something like when you put the "out" keyword in a Raycast function to store the raycast hit.
You are welcome! I am happy that you solved the issue :D $$anonymous$$eep it Up!!!
Huh? The answer doesn't make much sense besides the advice to check the documentation first.
This line:
PrefabUtility.RevertPrefabInstance(player go);
won't compile under no circumstances, no matter what "player" and "go" is. RevertPrefabInstance takes a single parameter of type GameObject. Therefore the code in the question is correct and should work.
However it's not clear what
it doesn't seem to be working
actually means. $$anonymous$$eep in $$anonymous$$d that the position / rotation are never reverted as you can actually read in the documentation.
Anyways this answer is wrong and misleading. This also has nothing to do or is in any way related to the "out" or "ref" keyword.
of course only using "go" will not Works. Only he knows on his code what the "go" means there and of course he used the right value and it Works. ¬¬
But your answer doesn't answer anything. You suggested this:
Change this PrefabUtility.RevertPrefabInstance(player);
TO THIS: PrefabUtility.RevertPrefabInstance(player go);
But this doesn't make any sense. The first line is the correct one and the one you suggested makes no sense at all.
Sorry for the confusion. Bunny is right, "(player go)" won't complie. What happened was that I had to switch my task and was unable to try the code... but since I wasn't using the "go" keyword I thought that was it. I didn't want to be ungreatful and decided to accept the answer before I forgot all about it, but I didn't had the chance to try it out. I'm new to Unity answers, and still a little clumsy. I still appreciate the help, Diukrone. Thanks to both.
Now I'm back to the task. To layout my problem (sorry if this is getting tedious by now): $$anonymous$$y code does compile and it doesn't thorw any errors, but the prefabs won't turn blue. I was hoping I could reconect the prefabs without going scene by scene and pressing the "revert" button.