Search Unity

  1. We are looking for your feedback about Templates! Tell us about your experiences by taking our survey.
    Dismiss Notice
  2. Curious about what's going to be in 2021.1? Have a look at the 2021.1 beta blog post.
    Dismiss Notice

SerializeReference Attribute?

Discussion in '2019.3 Beta' started by SonicBloomEric, May 15, 2019.

  1. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    382
    I noticed that the issue is now marked as fixed in 2021.1.0a5, so I tested it and the constructors of the serialized objects are now invoked as expected, but the property initializers are still ignored (values are lost) when entering play mode. I've also noticed that the title of the issue has been changed and it's no longer mention the field initializers. I've submitted another issue (Case 1293793) with the repro; really hope it'll eventually be fixed in 2019.4 LTS.



    @karl_jones @LeonhardP @unity_Etienne @alex-abreu-unity @MartinBarrette
     
  2. MartinBarrette

    MartinBarrette

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    23
    The issue which you are observing, is due to the backing field of the property being serialized in the editor to display those in the debug inspector. This is inconsistent as we are not serializing backing fields elsewhere currently. We will address this in the short term likely by disabling the support for backing field in the editor.
     
    oscarAbraham, Elringus and karl_jones like this.
  3. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    382
    Thank you for the response! I was able to reproduce the same issue with private fields, though; I've shared the details in the email, so I guess it'd be better to continue the conversation there.
     
  4. MartinBarrette

    MartinBarrette

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    23
    The reason that backing fields are serialized is to enable hot reloading of script during play mode. This is also the case for private fields as documented https://docs.unity3d.com/Manual/ConfigurableEnterPlayModeDetails.html step 3b. Changing the behavior would have impacts on lots of existing users. The workaround is to use [NonSerialized] to explicitly prevent the serialization of the content of those fields when entering play mode.
     
    phobos2077 likes this.
  5. fwalker

    fwalker

    Joined:
    Feb 5, 2013
    Posts:
    166
    But you still can NOT do this can you?
    The interface if showing the lable of the field "doable". But no box to drop a reference in

    Code (CSharp):
    1.  
    2. public interface IDoable {
    3. void DoIt();
    4. }
    5. public class Doer : MonoBehaviour, IDoable {
    6. public void DoIt(){};
    7. }
    8. public class Doer2 : MonoBehaviour, IDoable {
    9. public void DoIt(){};
    10. }
    11.  
    12. public class DoableContainer : MonoBehaviour
    13. {
    14.     [SerializeReference]
    15. public IDoable doable;
    16. }
     
    Last edited: Nov 24, 2020
    PixelLifetime and phobos2077 like this.
  6. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    402
    Try this.
    Code (CSharp):
    1.  
    2. public interface IDoable {
    3. void DoIt();
    4. }
    5. public class Doer : MonoBehaviour, IDoable {
    6. public void DoIt(){};
    7. public int i;
    8. }
    9. public class Doer2 : MonoBehaviour, IDoable {
    10. public void DoIt(){};
    11. }
    12.  
    13. public class DoableContainer : MonoBehaviour
    14. {
    15.     [SerializeReference]
    16. public IDoable doable = new Doer();
    17. }
    You should see the instance of Doer to the right from the label.

    But you right you can not change reference in editor with just unity default tools.

    To change references in editor you can
    use Odin Inspector
    or https://forum.unity.com/threads/serializereference-genericserializedreferenceinspectorui.813366/
    or create your own changer - drawer
    or convince Unity to implement that must needed feature :)
     
  7. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    382
    I see, thank you for the clarification! I've also noticed that making the field `readonly` excludes it from the serialization as well. Is that something we can rely on or [NonSerialized] attribute is the only proper way to do this?
     
  8. vertxxyz

    vertxxyz

    Joined:
    Oct 29, 2014
    Posts:
    46
    When using SerializedProperty is it possible to let DuplicateCommand or
    InsertArrayElementAtIndex to function as they previously did, where it actually duplicates the value?
    The current (and apparently intended) behaviour is that it duplicates by reference.

    I really don't want to have to find the field via reflection, duplicate that with a serializer, and then set that back. I have that code, it's just very unnecessary seeing as all the data is here and waiting. There just seems to be no API to do it, unless I'm missing something?
     
    Ghat-Smith and Jes28 like this.
  9. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    244
    Is there no way to serialize a System.Type with this? If I try to assign a type in a property drawer to property.managedRefeferenceValue it gives NotSupportedException from somewhere in the RuntimeType constructor.(It doesn't actually give me a proper traceback)
     
  10. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    49
    Hi, this might be of interest to people watching this thread. When the issue of instances being recreated was marked as fixed (https://issuetracker.unity3d.com/is...lds-lose-their-values-when-entering-play-mode), I was very happy and I tested it a lot. It works relatively well for me, except that instances are still recreated on Undo/Redo.

    I've just noticed that Unity devs were able to reproduce the bug and added it to the issue tracker. Here it is in case anyone wants to follow or vote on it: https://issuetracker.unity3d.com/is...difiying-any-field-and-then-doing-ctrl-plus-z
     
    TextusGames likes this.
  11. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    332
    Im using 2019.4.17f1 and noticed something weird. I have a ScriptableObject with a [SerializedReference] List<SomeAbstractType>, some of these derived Types contain a enum.
    I wrote a custom editor to add instances to the list in the inspector.

    Now the weird thing is that the new inserted instance shows all enum fields as number instead of a string value, but if I click somewhere else in the editor and reselect the ScriptableObject asset it suddenly shows the enum members as strings again.

    Does anyone know if there is some way to force the same logic a focus change does, to show the string values directly after my add item logic? I tried to call the Repaint() method of the editor, but it didn't help, only a real focus change seems to work.
     
    Last edited: Jan 12, 2021
    Ghat-Smith likes this.
  12. MartinBarrette

    MartinBarrette

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    23
    If you use https://docs.unity3d.com/ScriptReference/SerializedProperty-managedReferenceValue.html to insert those new instances the fields will show up as Enum. We have fixed an issue related to this which will be backported in older versions.
     
    Ghat-Smith, R1PFake and karl_jones like this.
  13. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    332
    Thanks for the hint, I can confirm that it works with my custom editor by setting the managedReferenceValue instead of adding directly to the list
     
    MartinBarrette likes this.
  14. Yany

    Yany

    Joined:
    May 24, 2013
    Posts:
    33
    Actually the SerializeField issue still exists: https://issuetracker.unity3d.com/is...leting-last-array-item-and-then-pressing-undo

    It sounds like it's a separate problem, but in the notes of the issue down below you can see that's the same it was discussed last year(!): "When deleting array element Editor throws this error: Could not reach source property name 'entries.Array.data[0]' while extracting diffs, the reference does not exist in the source serialized data"

    So it's not an UNDO issue. Could you press a vote on it please? Thank you.
     
    Last edited: Feb 2, 2021
    oscarAbraham likes this.
  15. MartinBarrette

    MartinBarrette

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    23
    https://issuetracker.unity3d.com/is...642718.95653275.1612321609-6940258.1548978882
    FYI this issue has been fixed locally and the fix is being reviewed and validated currently.
     
    Yany, oscarAbraham and TextusGames like this.
  16. Yany

    Yany

    Joined:
    May 24, 2013
    Posts:
    33
    That's very good news, thank you.
     
unityunity