Search Unity

AddPersistentListener not saving in scene

Discussion in 'Scripting' started by noethis, Apr 3, 2020.

  1. noethis

    noethis

    Joined:
    Nov 11, 2013
    Posts:
    129
    I'm using AddPersistentListener in script to dynamically fill out a UnityEvent in the editor (not at runtime). It's working correctly and properly shows up in the inspector and everything, but as soon as I reload the Unity scene it's not there any more. I can't get it to commit the changes to the scene. I've tried calling Undo.RecordObject and EditorSceneManager.MarkSceneDirty but neither helps.

    Any ideas? Is this a serialization issue?

    Code (CSharp):
    1.  
    2.         if (unityEvent.GetPersistentEventCount() == 0) {
    3.             if ( signals == null ) {
    4.                 return;
    5.             }
    6.             foreach (Signal s in signals) {
    7.                 foreach (var c in s.target.GetComponents(typeof(Component))) {
    8.                     var targetInfo = UnityEvent.GetValidMethodInfo(c, s.method, new System.Type[0]);
    9.                     if (targetInfo != null) {
    10.                         UnityAction methodDelegate = Delegate.CreateDelegate(typeof(UnityAction), c, targetInfo) as UnityAction;
    11.                         UnityEventTools.AddPersistentListener(unityEvent, methodDelegate);
    12.                     }
    13.                 }
    14.             }
    15.         }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I'm not sure how this would work. Remember you can only save to disk references that point to things that are already saved to disk.

    So at editor time, I'm not sure how you would say "Hey, hook me up to this prefab's instance of this function." Obviously it's possible because Unity does it all over the place, but I just don't know what the magic sauce is.

    I'm guessing in this case you're referencing the object in-scene, rather than the prefabbed version of it.
     
  3. alsharefeeee

    alsharefeeee

    Joined:
    Jul 6, 2013
    Posts:
    80
    Were you able to solve it? Facing the same problem right now.
     
  4. noethis

    noethis

    Joined:
    Nov 11, 2013
    Posts:
    129
    Hmm, don't remember unfortunately but I'm guessing I ended up dropping it / trying another approach.
     
    alsharefeeee likes this.
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,935
    Make sure you are dirtying the scene/object you're adding the listener too.
     
  6. alsharefeeee

    alsharefeeee

    Joined:
    Jul 6, 2013
    Posts:
    80
    I don't get you! What do you mean?
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,935
    Unity doesn't automatically know that it needs to save an object if you change it's values via code. You specifically have to let it know it's been modified, known as 'dirtied', so that it can write the changes to disk.

    The most common method of which is EditorUtility.SetDirty.

    When changing values via the inspector, this automatically dirties the object. But via code, it needs to be done manually.
     
    alsharefeeee likes this.