TylerTemp / SaintsField

A Unity Inspector extension tool focusing on script fields inspector enhancement
MIT License
148 stars 9 forks source link

`[Button]` Function to find and assign references to prefab doesn't save? #54

Closed KamilTheDev closed 1 month ago

KamilTheDev commented 1 month ago

If I have this code:

[SerializeField] private RectTransform rectTransform;
[SerializeField] private TMP_Text message;

[Button]
private void GetRefs()
{
    rectTransform = transform.GetComponent<RectTransform>();

    message = transform.Find("Message").GetComponent<TMP_Text>();
}

Clicking the button assigns fields correctly, but they're never actually saved to the prefab. If you exit and go back into the prefab, the fields are empty.

Edit: If I make some change manually, then it will save properly. Is there some function I can call to force Unity to save the prefab?

KamilTheDev commented 1 month ago

Calling EditorUtility.SetDirty(gameObject); seems to be the correct way to mark prefab as modified.

TylerTemp commented 1 month ago

Calling EditorUtility.SetDirty(gameObject); seems to be the correct way to mark prefab as modified.

Yes, but it's more recommended to use Undo.RecordObject(gameObject, name) before you make a change so you can undo it.