Search Unity

  1. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice

SendMessage cannot be called during Awake, CheckConsistency, or OnValidate

Discussion in '5.5 Beta' started by Hyp-X, Aug 30, 2016.

  1. Hyp-X

    Hyp-X

    Joined:
    Jun 24, 2015
    Posts:
    390
    Hi,

    With 5.5.0b1 I get 999+ of this warning when loading one of our map in the editor:

    SendMessage cannot be called during Awake, CheckConsistency, or OnValidate

    They come from the following calls (at least):

    UnityEngine.Object:Instantiate(GameObject)
    UnityEngine.GameObject:set_layer(Int32)
    UnityEngine.Transform:set_parent(Transform)
    UnityEngine.Transform:set_localRotation(Quaternion)
    UnityEngine.Transform:set_localPosition(Vector3)
    UnityEngine.Transform:set_localScale(Vector3)
    UnityEngine.Renderer:set_enabled(Boolean)

    The calls all seem to succeed but they spam the console with these messages.

    Is this a bug?
    Should we stop calling these functions from OnValidate?
     
  2. maleeb

    maleeb

    Joined:
    Apr 12, 2015
    Posts:
    1
    same here

    SendMessage cannot be called during Awake, CheckConsistency, or OnValidate
    UnityEngine.MeshFilter:set_sharedMesh(Mesh)
    Destructible2D.D2dDestructible:UpdateMesh() (at Assets/Scripts/D2dDestructible.cs:1566)
    Destructible2D.D2dDestructible:OnValidate() (at Assets/Scripts/D2dDestructible.cs:1513)
     
  3. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    2,483
    Hi,
    The warning message was added recently but the underlying problem is older. While the SendMessage call on OnValidate can work without issues, it can also fail, depending on specific circumstances. At that point in time we can not guarantee to have a valid, fully initialized object. Therefore calling SendMessage is not safe and should be avoided. Relying on SendMessage during the mentioned phases might cause subtle (or less subtle) bugs and is not supported.
     
  4. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    I also get this by dropping a UI Slider in a scene and scrubbing the Value in the Inspector.
     
  5. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    @LeonhardP any update on this? I still get warnings when scrubbing a slider in the Editor.
     
  6. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    2,483
    Hi Gizmoi,
    The warning will be disabled for 5.5 and the parts of the UI system that cause it will be rewritten for 5.6.
     
    Gizmoi likes this.
unityunity