Search Unity

Public property in inspector

Discussion in 'Scripting' started by JoelGab, Jun 6, 2019.

  1. JoelGab

    JoelGab

    Joined:
    Jul 17, 2018
    Posts:
    25
    So I have a MonoBehaviour, that has a bunch of private serialized fields.
    And then some public properties that set those private fields.

    I have an editor window that manages a set of components in a more structured way (so basically, no need to dig through the hierarchy).

    However the public setter does not seem to update the serialized float in the inspector, is there any way I can do this without just making everything public?

    I debugged in VS and it shows the value itself updated, just not the serialized value.

    here's a small snippet:
    upload_2019-6-6_16-21-45.png
     
  2. ProtagonistKun

    ProtagonistKun

    Joined:
    Nov 26, 2015
    Posts:
    352
    assuming from what i can see in the snippet this is fine,
    but you might wanna mark your object as dirty or use serializedObject.ApplyModifiedProperties
    Code (CSharp):
    1. if(GUI.changed)
    2. {
    3.     serializedObject.ApplyModifiedProperties();
    4.     //or
    5.     EditorUtility.SetDirty(target);
    6. }
     
    JoelGab likes this.
  3. JoelGab

    JoelGab

    Joined:
    Jul 17, 2018
    Posts:
    25
    Hey @ProtagonistKun , Thanks for the reply.

    So it turns out it was my bad, I have a window that is either a "Builder" or a "Manager". A builder if the rig isn't in the scene, a manager if it is.

    The issue was, I was applying this to the prefab, thinking it was the in-scene reference.

    I had some code that was meant to assign _rig to the correct object depending on the state (builder/manager). However, it always fell into an if statement assigning it to the prefab and not the in-scene reference.

    Sorry for the post, it was just me forgetting to update the condition. :)
     
  4. ProtagonistKun

    ProtagonistKun

    Joined:
    Nov 26, 2015
    Posts:
    352
    Ah right, not to worry. I ve had that one before trying to manage scriptable objects and applying their data to prefabs...
     
    JoelGab likes this.