Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Correct replacement for changing Unity values in OnValidate()?

Discussion in 'Editor & General Support' started by Arkade, Feb 3, 2020.

  1. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    621
    TL;DR What's the Unity-approved code-based way to make changes to serialized values during Edit time for simple components rather than in OnValidate()? (simple i.e. without a custom Editor)

    In the past, something like UI layout code might update its children's positions during edit-time in OnValidate(). That now generates hundreds of errors like this:

    OnValidate-errors.png

    Many of these in this example are from open-source RadialLayout here but I have plenty of similar from other sources including my own code. Obviously UI code isn't the only thing that might do this. Say you want to set some serialized field to a known child component, you might check it and use GetComponent() if it's unset.

    According to posts like this, we shouldn't rely on this approach. There's a 'fixed' bug in 2019.1 but I'm seeing hundreds of these in 2019.3.0f6. But maybe a better questions is:

    What's the 'correct' way to do that now?

    Thanks
     
  2. pryankster

    pryankster

    Joined:
    Aug 6, 2015
    Posts:
    3
    I'm seeing this in an OnValidate() function where I activate/deactivate child objects (i.e.: childObj.setActive(true)). Is there a correct way to do this?
     
  3. pbritton

    pbritton

    Joined:
    Nov 14, 2016
    Posts:
    76
    An answer to this would be greatly appreciated.
     
  4. AdamBebko

    AdamBebko

    Joined:
    Apr 8, 2016
    Posts:
    22
    Yes I'm running into the exact same problem. Trying to activate/deactivate a child in editor mode based on when a bool is flipped. In OnValidate()

    Edit: I have a solved workaround

    Child's script:

    1) move child one level deeper in hierarchy by creating a new empty gameobject. so you now have Original parent > new child > old Child
    2) create new script on new child. set to [ExecuteInEditMode]
    3) new public bool isActive.
    4) add this to update method:

    New Child Script:

    Code (CSharp):
    1. public bool isActive;
    2. public Transform oldChild;
    3.  
    4. void Update() {
    5.     oldChild.SetActive(isActive)
    6. }
    5) Modify parent script

    Parent Script:
    Code (CSharp):
    1.  
    2. public bool newChildIsActive;
    3. public Transform newChild
    4.  
    5. void OnValidate() {
    6.     newChild.isActive = childIsActive;
    7. }
     
    Last edited: Apr 9, 2020
  5. LeFx_Tom

    LeFx_Tom

    Joined:
    Jan 18, 2013
    Posts:
    26
    This is a hacky workaround
    The forum is full with questions about this basic functionality...will someone @Unity maybe have mercy and come down to us and explain for once? There is no official answer anywhere in those posts, just user looking for help.
     
unityunity