Unity Editor does not saves prefab variable (Texture2D) which was generated and assigned by editor script

I have a lot of parts in my sandbox construction game and I created an editor script which generates icon for each object proceduraly (in order to display it in construction menu).

I generated a texture using a screenshots from camera and saved it in PNG file somewhere in assets folder of my Unity project.

After that, I extracted this saved texture from the assets and assigned it into variable of my prefab using the following code:

// Extract Texture2D from assets
Object SavedImage = AssetDatabase.LoadAssetAtPath ("Assets/Icons (Generated)", typeof (Texture2D));

// Assign texture to prefab variable
SomeMyClass.IconTexture = (Texture2D)SavedImage;

An this works. Generated texture now assigned to prefab variable and visible in the inspector window. For example, I can click on variable and texture in assets will be highlighted for a second. Seems all ok.

But

I pressed File - Save project (just to be sure) closed Unity, opened it again…

And approximately half of prefabs now have empty “.IconTexture” variable.
Yes, not all of them, only some of them lose assigned texture, others - still have it. Magic. O_O

Besides that, all generated texture still in folder where it was saved (Assets/Icons (Generated)).

Another note. I can launch my generator again. All prefabs will got its assigned texture. But if I select a prefab, click on small circle right from the variable (select button) and click on the same texture which already selected and after that, restart Unity again - this prefab will save assigned texture. (But after I will regenerate textures, it will lose it again!)

So, seems like Unity saves variable of prefabs only when I assigned something to it manually via inspector, using a mouse. And saves it only sometimes if variable was assigned by editor script.

Anyone know what a magic happens here? :slight_smile:

Can you see if the answer to this question helps in this case?