3
\$\begingroup\$

I am creating an editor tool that requires me to enable / disable components and or gameObjects via script while in edit mode. Currently everything that has to execute in edit mode is called from OnValidate()

When calling either gameObject.SetActive(...) or component.enabled(...) from OnValidate I get the following warning:

SendMessage cannot be called during Awake, CheckConsistency, or OnValidate (Scrample: OnBecameInvisible)
UnityEngine.StackTraceUtility:ExtractStackTrace ()

From what I can tell from googling, in particular this thread, this is expected behaviour.

If that is the case, what would be the best way to enable / disable stuff via script when in edit mode?

Thanks

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

The way I got around this was to set a bool in OnValidate that is checked in Update. You will need to add the compiler directive [ExecuteAlways] in front of the class declaration. This causes the Update method to run in the editor.

void OnValidate()
{
    if (bInspectorVar == true)
    {
        //set flags for use in Update
        bUpdateFlag1 = true
    }
}

void Update()
{
    if(bUpdateFlag1==true)
    {
        //do instantiation/destroyimmediate here
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Thanks, DMGregory \$\endgroup\$ Jun 1, 2022 at 23:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .