- Home /
Changes made in prefab mode via script aren't marking the object as Dirty
Hi there,
We have a set of tools which we use to allow artists to safely edit prefabs without breaking any references. The tools simply destroy some GameObjects in a prefab, replace them with new ones, and then update the appropriate references. However, since the change to prefabs, making these changes in Prefab Mode don't register and the Save button never activates (although the prefab updates correctly). It also doesn't trigger the autosave. Making the same changes by hand in the same mode does. I've tried marking the prefab as dirty, but that doesn't activate the save button either. The PrefabUtility options seems to save the object as an override, but we don't want that, we want them to behave as they do when you edit it manually. We are simply doing this from an inspector window:
public void LoadDummyCharacter( string dummyCharacterId )
{
CharacterBase newDummyChar = LoadCharacterFromResourceFolder( dummyCharacterId );
if( myTarget.DummyCharacter != null )
{
DestroyImmediate( myTarget.DummyCharacter.gameObject );
}
if( newDummyChar != null )
{
myTarget.AssignDummyCharacterReference( newDummyChar );
myTarget.DummyCharacter.transform.parent = myTarget.transform;
myTarget.DummyCharacter.transform.localPosition = Vector3.zero;
myTarget.DummyCharacter.transform.localScale = Vector3.one;
myTarget.DummyCharacter.transform.localRotation = Quaternion.identity;
myTarget.DummyCharacter.gameObject.SetActive( true );
}
}
Any thoughts on what I can do to make the prefab detect that it has been changed, so we can save it?
Answer by Invertex · Sep 01, 2019 at 12:16 AM
The Prefab Editor is in a sense its own "scene", so you have to actually not just mark the object dirty, but also call:
UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(gameObject.scene);
Then the "save" button will show as clickable after your edit.
Your answer
Follow this Question
Related Questions
Staged Prefab can't "Apply" after adding using Gameobject as a variable 1 Answer
Prefab not loading in data from Inspector 1 Answer
Distribute terrain in zones 3 Answers
Saving a prefab through code to a folder 0 Answers
Multiple Cars not working 1 Answer