Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Undo Redo 4.3.4

Discussion in 'Scripting' started by KB73, Apr 10, 2014.

  1. KB73

    KB73

    Joined:
    Feb 7, 2013
    Posts:
    234
    Has anyone else has much luck with the newish Undo system? Docs are sparse.

    I am able to create an object find and register it with Undo.RegisterCreatedObject( obj, "blah");

    All good, at this stage if i just do Undo/Redo from the menu the item is created and destroyed as expected.

    If i then go and destroy the object later with Undo.DestroyImmediate, the system gets in a pickle.
    Seems something doesn't get patched up right.

    I started with ScriptableObjects and thought maybe due to that, switched to Monobehaviour, still no luck. I wrote a super quick sample to test and it still occurs.

    So, given the docs are quite sparse on the issue, what is the expected flow in code when you forcibly destroy an item and then use Redo from the menu to create it?

    I would expect something like..

    Undo.DestroyImmediate(obj); <- this creates an undo action

    i also tried
    Undo.Record( container, "blah");
    UnityEngine.Object.DestroyImmediate(obj);
    container.Remove(obj);

    And then doing Redo should recreate ok?

    Either way, it's not behaving. I'm not using anything fancy.

    None of this code is used for any kind of serialization to disk, just GameObjects, floats, List<> etc..no dictionaries or anything. Everything has the Serializable / SerializeField attribute.

    Things become interesting when the items are added into a List<> generic..even if the items are fully serializable. The list appears to work fine if you just add an item and then undo/redo in the menu.

    If you decide to remove something from the list via your own code or via the Undo system Destroy, it all goes belly up, the reference is lost, the list container contains a null and then the object is re-enabled and then you need to 're-add' ? ..Is the expectation that you have to manually patch your items back into lists where needed? My expectation was the List<> container would be fully serialized out? OnEnable isn't fully reliable either from what i have seen.

    I'm at the stage where i am literally going to have to roll my own and pump it through an internal load/save system which kind of defeats the purpose.

    Going around in circles with this. Any input appreciated.

    cheers
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Try this:
    Code (csharp):
    1.  
    2. Undo.Record( container, "blah");
    3. container.Remove(obj);
    4. Undo.DestroyImmediate(obj);
    5.  
    Otherwise you are removing the faux null reference.
     
  3. KB73

    KB73

    Joined:
    Feb 7, 2013
    Posts:
    234
    Thanks for the reply, the list container actually contains the right number of elements on the redo but the element is missing...i've been changing a ton of code, let me work back and see if it is something else i've done.
     
  4. KB73

    KB73

    Joined:
    Feb 7, 2013
    Posts:
    234
    Ok fixed, so it looks like i had 2 things wrong, i had the Remove after the Destroy as you showed, tired eyes..

    And i was stupidly using the component script in the container and not the gameobject it was attached to ugh..

    Thanks for the tweak.
     
  5. KB73

    KB73

    Joined:
    Feb 7, 2013
    Posts:
    234
    Hmm, still no dice. I thought it was working, the inspector says the item is there but the underlying list which was part of the object in the RecordObject has a null in it. So, the thing looks like it has been serialized in but the reference has been decoupled....:/

    I've emailed the Unity guys as i've exhausted pretty much every avenue on this now.
     
  6. KB73

    KB73

    Joined:
    Feb 7, 2013
    Posts:
    234
    And for reference, if i don't put the entity in a list, it works 100%
     
  7. KB73

    KB73

    Joined:
    Feb 7, 2013
    Posts:
    234
    Ok, it's all fixed finally - 2 further issues

    The data binding was messed up in 4.3.4, in 4.5 beta 6 it appears to have been fixed, with issues related to it in the release notes.

    This issue was masking another issue which was not helping either ( the way i was instantiating a prefab and referencing a component within )..

    Anyway all good....finally.