I am trying to add a GameObject as a sub-asset of a ScriptableObject.
Try #1
I have tried to do it that way :
GameObject go = new GameObject("object name");
go.hideFlags = HideFlags.HideInHierarchy;
AssetDatabase.AddObjectToAsset(go, myAsset);
but when I press Play, I get those errors :
-
Can’t destroy Transform component of ‘object name’. If you want to destroy the game object, please call ‘Destroy’ on the game object instead. Destroying the transform component is not allowed.
-
Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)
Try #2
and that way :
GameObject go= new GameObject("object name");
GameObject prefab = PrefabUtility.CreatePrefab("Assets/temp.prefab", go);
prefab.hideFlags = HideFlags.HideInHierarchy;
AssetDatabase.AddObjectToAsset(prefab, myAsset);
With this code, I get an error on the call to AssetDatabase.AddObjectToAsset :
- You may not change the path if an object is already peristent in another one
Is it possible to add a gameobject (as a prefab or not) as a sub-asset of a ScriptableObject?