Search Unity

Saving transforms into asset = obliterated Unity lol :D

Discussion in 'Editor & General Support' started by IgorAherne, Oct 31, 2014.

  1. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    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" 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

    How to store the transform form the objects in scene as an asset?
    We broke it )
     
    Last edited: Oct 31, 2014
  2. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    Somebody plz help, I beg you :<
     
  3. billykater

    billykater

    Joined:
    Mar 12, 2011
    Posts:
    329
    Hi,

    Short answer is that you can't save a transform in a scriptableobject as an asset.

    Each Object in unity has a something I like to call the "serialization root", This is the place the object gets serialized to and if this object gets destroyed all objects depending on it will be "broken". For all components (Either your own or builtin ones like transform) this root is the gameObject it is attached too (and in the last instance the scene it is in or the prefab). Additionally unity has some internal state of the object (e.g. to allow you to do transform.parent). This is why all this "weird" errors are showing up. Simple example: if unity would indeed allow you to reference a transform in a scriptablebject what should happen in the case you access the saved transform of scene a in scene b? Even worse, what happens if the transform is a child of another transform and you try to attach a gameobject to it.

    Solution
    Depending on your needs
    • If you want to only retain the position/rotation/scale part of the transform just create a serializable class that holds these values and write a custom property drawer that still lets you drag in scene transforms.
    • If you want to attach things to these transforms it gets more complicated. You will need to come up with a way to find the transform by some identifier of your own making.

    Lastly some helpful links from my unity collection which should also contain some answers to the question you asked in the dictionary thread yesterday:
    Lastly some forum advice: bumping your thread after just two hours is a little bit short, give other users some time ^^.
     
    IgorAherne and superpig like this.