Is there a reason? ScriptableObjects not accepting HideAndDontSave flag.

I’ve decided to keep main, fixed attributes of my game’s bullets and enemies as assets. Namely, as ScriptableObjects. As many tutorials and guides around suggests, I am to set the hideFlags as HideAndDontSave on the OnEnable method of my ScriptableObject class. Since I am lazy, I am using this wiki code to instantiate my assets:

http://wiki.unity3d.com/index.php?title=CreateScriptableObjectAsset

And then, I get as a result this error message:
o->TestHideFlag (Object::kDontSave) (options kAllowDontSaveObjectsToBePersistent) == 0

A new asset is created. It has the icon of that white paper, and the ScriptableObject fields are dark gray so I cant edit them. If I select anything else, when I select back my new asset I have one empty inspector tab, just like the one you have when you create a new prefab with the right mouse button menu.

If I change hideFlags to none, the asset is created and I have no error messages. If I use the DontSave flag, the same error appears. I’ve searched for this error message and only found this:
http://forum.unity3d.com/threads/183375-Can-t-AssetDatabase-CreateAsset-for-a-ScriptableObject-w-hideFlags-HideAndDontSave

If it makes any difference, follows the codes

// BulletDataBase.cs
[System.Serializable]
public class BulletDataBase : ScriptableObject {
	public float damage;

	protected virtual void OnEnable() {
		this.hideFlags = HideFlags.HideAndDontSave;
	}
	
	protected virtual void OnGUI() {
		damage = EditorGUILayout.FloatField(damage);
	}
}
// BulletDataBaseAsset.cs
public class BulletDataBaseAsset {
	[MenuItem("Assets/Create/Bullet Data Base Asset")]
	public static void CreateAsset() {
		ScriptableObjectUtility.CreateAsset<BulletDataBase>();
	}
}
1 Like

I am curious which tutorials suggest to hide a scriptable object. To me this doesn’t really make sense. You create it to become an asset, meaning it is stored in a file. Why would you want to hide a file and even more important why do you want to create a file and not save it?

A quick reference is this one:
http://forum.unity3d.com/threads/155352-Serialization-Best-Practices-Megapost

Also, there was this post recently:
http://forum.unity3d.com/threads/207379-ScriptableObject-lifetime-and-BuildPlayer

Also, from the reference guide:

“A combination of not shown in the hierarchy and not saved to to scenes. This is most commonly used for objects which are created by scripts and are purely under their control.”
And for DontSave
“The object will not be saved to the scene. It will not be destroyed when a new scene is loaded. It is your responsibility to cleanup the object manually using DestroyImmediate, otherwise it will leak.”

It says that the object is not saved to scene, but does not say anything about asset files. Also, for now it is unclear for me if this (rather hard to read) error is recent or if I am one of the very few people who tried to make this and had this problem. Since there is space for ambiguity and misinterpretation, I’m just trying to really understand these flags.

Edit: quick checking the tutorials here. They never explicitly said to use the ‘dont save’ flags altogether with AssetDatabase. I have jumped to early conclusions. But I’m still curious, since creating asset afaik is not saving to scene.

1 Like

Your scriptable object is an asset. As such it is saved within a file and not in the scene. When you create an asset, it is in no scene. It is like a 3d asset or a texture. That’s why HideAndDontSave makes no sense in that context. The most useful application for scriptable objects is to store custom data in a file and thus it is not needed or possible to use HideAndDontSave.

It is another topic when you create a game object in the editor that doesn’t need to be saved.

I see. Thanks for the clarification.