- Home /
Saving transform component into asset = destroys unity
I am struggling saving the transforms from the selected objects into an asset.
My main asset is a scriptable object with a dictionary in it. Granted that dictionary doesn't serialize properly, I help it with the serialization callbacks provided by Unity. Its keys and values are flushed into 2 lists and then the dictionary gets reconstructed from the data in those lists. More info on that here
Values of the dictionary are scriptable objects (Node), and the keys are the Transform objects.
As I am adding more values into the dictionary I am adding them to the main asset file. During serialization, I can see that the values are nicely buffered and then the dictionary is re-constructed just fine.
However, as I am trying to add transforms of the objects selected from the sceneview, into the main asset, it works ok. But as soon as I enter game mode - unity crashes. next time I enter Unity, I also receive an error "Transform component deleted: GameObject pointer is invalid". When I look into my asset file, it still encompasses those scriptable objects from previous time and the transform objects are gone, but their icons are still present (with no data shown in the inspector as I click on them). When I start to add new transforms (one transform per each gameObjects selected in the sceneview), it all seems to refresh, the icons of old transforms are gone and the new ones are seen in the main asset. If I click on those transform components, I can see appropriate transform data in the inspector. Then everything repeats - enter game - crash unity.
How to solve that issue? Maybe it's because of two consecutive "AddObjectToAsset"? Or is it because I need to save the whole gameObject which carries the transform component?
EditorUtility.SetDirty(mainDictionary);
AssetDatabase.AddObjectToAsset(selectedTransform, mainDictionary);
AssetDatabase.AddObjectToAsset(transformNodeScriptObj, mainDictionary);
AssetDatabase.SaveAssets();
EDIT:
If I store gameObjects instead of transforms (of course having modified Dictionary types and the code), I am getting several errors "Persistent object inconsistency" some info on that and then another ones: "Can't destroy Transform component of 'mainDictionary' If you want to destroy the gameObject please call 'Destroy' on the game object instead. Destroying the transform component is not allowed. "
Then "Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)"
EDIT:
I decided to create an asset which contains a transform of the object selected in the scene viewport. I can see that the Position, Rotation and Scale are displayed in the inspector if I select the asset. However, once again, Unity crashes once I enter the gameplay mode. When I come back I see that the transform asset is empty (only icon remains).
If I save the scene, so that the transform's gameObject remains in a scene, when I come back unity will crash before its open, so there is not way to get into the scene
(manually deleting the transform asset (the one we snatched from that gameObject) via Windows Explorer) doens't help loool :D. But deleting the scene does
We broke it )
Your answer
