Unity - Scripting API: AssetDatabase.AddObjectToAsset

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static void AddObjectToAsset(Object objectToAdd, string path);

Parameters

Parameter Description
objectToAdd Object to add to the existing asset.
path Filesystem path to the destination asset.

Description

Adds objectToAdd to an existing asset at path.

Please note that you should only add objects to '.asset' assets, imported models or texture objects for example will lose their data.

All paths are relative to the project folder.

Note: You can not add GameObjects; use PrefabUtility instead.


Declaration

public static void AddObjectToAsset(Object objectToAdd, Object assetObject);

Parameters

Parameter Description
objectToAdd Object to add to the existing asset.
assetObject Destination asset.

Description

Adds objectToAdd to an existing asset identified by assetObject.

Note: Having objectToAdd on disc before calling AddObjectToAsset will generate an error (ex. trying to add "MyMaterial" to an existing asset): "Couldn't add object to asset file because the Material 'MyMaterial' is already an asset at 'Assets/MyMaterial.mat'!"

Note: You have to serialize the changes in memory to disk.
This is because assets that have been modified in memory, must be saved to disk.
Failling to do this will produce an inconsistent result warning, as in-memory modifications to the asset will be lost.