So basically I’ve a scriptable object which I freshly create. Upon creation, it works fine, but as soon as I include my prefab into my variable called “assetReference”, it triggers the following code below which is to Rename the ScriptableObject to have the identical name as the prefab. I have tested it and can 100% confirm the crash is due to the RenameAsset() function.
Note** this crash only happens once per newly created scriptable object instance. After the crash, re-opening Unity Editor and dragging a prefab into the old scriptable object instance which caused the crash, Rename Assets works and successfully rename the asset. I’m not sure why it crashes ONLY if the scriptableobject instance is freshly created. Please advice.
Using AssetDatabase.Refresh() and AssetDatabase.Save() also does not resolve this issue. Is this a bug or I cannot simply call a RenameAsset() from the object to be renamed itself?
#if UNITY_EDITOR
string guid;
string assetReferenceName;
Object obj;
private void OnValidate()
{
obj = this;
if (assetReference.RuntimeKeyIsValid())
if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(obj, out guid, out localId))
{
assetReferenceName = AssetDatabase.LoadAssetAtPath<GameObject>(AssetDatabase.GUIDToAssetPath(assetReference.AssetGUID)).name;
AssetDatabase.RenameAsset(AssetDatabase.GUIDToAssetPath(guid), assetReferenceName);
}
}
#endif
OnValidate happens as inspectors are updated, which is a threaded process. It generally should not be operating on anything outside it’s own object, hence the instability you’re experiencing.
This sort of work should be done with a custom inspector.
As above, OnValidate does warn of various things you shouldn’t be doing inside it however, nothing in Unity should crash so even if you take measures to workaround any crash, I would suggest reporting it as a bug. Crashes always have a priority, especially if a regression of any kind.
So just to understand more, do you mean that I should report this as a bug even though I’m not suppose to call RenameAsset in OnValidate as what you guys have advised?
I didn’t advise you either way on calling it but as I clearly said above, it shouldn’t crash. In all cases of a crash, it’s a bug report. Suggesting something shouldn’t be called doesn’t mean it should crash Unity. Exceptions are expected but those are not crashes.
Yes I’m aware of Custom Inspector coding and have resolved the issue based off the feedback above from you and @MelvMay by writing the custom editor code below. I didn’t assume that OnValidate would have been the issue, hence did not think of making a custom editor/inspector just for this use case.
Thanks again to the both of you, no more editor crashes now.
string assetReferenceName;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (!prefabData.assetReference.AssetGUID.IsNullOrEmpty())
{
assetReferenceName = AssetDatabase.LoadAssetAtPath<GameObject>(AssetDatabase.GUIDToAssetPath(prefabData.assetReference.AssetGUID)).name;
AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(prefabData.GetInstanceID()), assetReferenceName);
}
else if (prefabData.asset != null)
{
AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(prefabData.GetInstanceID()), prefabData.asset.name);
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}