Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

SendMessage cannot be called during Awake, CheckConsistency, or OnValidate

Discussion in 'Scripting' started by turiyag, May 14, 2022.

  1. turiyag

    turiyag

    Joined:
    May 13, 2022
    Posts:
    2
    Hey, I'm not sure why this warning happens, but I'm a new user to Unity, just trying it out, watching a great tutorial on Procedurally generated meshes:



    ...and I got a warning message, and I found an old forum post about it:

    https://forum.unity.com/threads/sen...-awake-checkconsistency-or-onvalidate.428580/

    But nobody had a solution that worked for me. (I'm in Unity 2021.3.2f1)

    The error was:

    "SendMessage cannot be called during Awake, CheckConsistency, or OnValidate"

    And I just wanted to say, the solution came to me while watching another tutorial about Input and Physics:



    ...where he said how you should listen for Input in the Update function, but apply Physics in the FixedUpdate function. This seemed like a related problem.

    Just set a boolean flag to update the Mesh in the next Update loop. That's it, just, in OnValidate(), just

    ```
    private void OnValidate() {
    _shouldUpdateMesh = true;
    }

    private void Update() {
    if (_shouldUpdateMesh) {
    // ... actual Mesh logic
    }
    }
    ```
     
    Last edited: May 15, 2022
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    The warning itself tells you exactly why and what you need to stop doing. :)

    The "solution" is to avoid doing what the warning is warning you about. :)

    Well yes, defer doing what you're doing in the OnValidate.

    The thing is, it sounds like something you're indirectly doing that itself is sending/broadcasting a message which can make it far from obvious for sure. You have to be careful in OnValidate and try to only do simple verification and correction of things like local properties etc. Dealing with objects can be problematic if they are doing stuff that isn't compatible at times during validation.

    You are free to post what you like as long as you like. If you're curious the Code of Conduct is here.
     
  3. turiyag

    turiyag

    Joined:
    May 13, 2022
    Posts:
    2
    I mean...suuuure, it says that some function I'm not calling directly cannot be called in the place that I'm indirectly calling it. But, at least for a novice like me, it doesn't suggest what I should do. Something like:

    SendMessage should not be called during Awake, CheckConsistency, or OnValidate. Defer complex logic to Update because OnValidate is only supposed to be used for simple verification and correction of things, because <REASON>. Consider maybe setting a flag and handling it in a different function.

    To be honest, I still don't understand WHY I can't do complex things in those three functions. I just understand that I'm not supposed to. In other frameworks (like web development) you CAN do things like verify a password every time the user presses a key in the password box. You SHOULDN'T, obviously, but there's no fundamental reason why you COULDN'T.