Search Unity

Send message cannot be called during Awake, CheckConsistency, or OnValidate error message

Discussion in 'Scripting' started by FerdowsurAsif, Aug 29, 2019.

  1. FerdowsurAsif

    FerdowsurAsif

    Joined:
    Feb 7, 2017
    Posts:
    288
    "Send message cannot be called during Awake, CheckConsistency, or OnValidate"

    I keep getting this warning message. It doesn't cause any problem but is filling up the log. Is there any way to disable this warning?



    It only happens when I do stuff during OnValidate() calls. On these lines on different sripts.

    Code (CSharp):
    1. GameObject prefab = Instantiate(characters[i].prefab);
    Code (CSharp):
    1. obj.transform.SetParent(transform);
    Code (CSharp):
    1. obj = new GameObject();


    Using Unity version: 2018.3.12f1


    Edit: I know I can disable some common warnings using stuff like this.
    #pragma warning disable 0168
    . But I don't know how to look for warning number for the error I am getting
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Well this is a runtime warning, not a compiler warning, so this doesn't apply. If it applied then the number you need would be a part of the message.

    I don't know why you're getting a warning like that on that code, though. That's strange.
     
    FerdowsurAsif likes this.
  3. FerdowsurAsif

    FerdowsurAsif

    Joined:
    Feb 7, 2017
    Posts:
    288
    Yea, most of the search result for this error seems like from a ui slider. I added slider to test it and not getting any error from that. I haven't tested using different unity versions.

    Seems like I can avoid the warning by using delay call
    EditorApplication.delayCall += () => MyMethod();
    as a temporary fix.
     
  4. FerdowsurAsif

    FerdowsurAsif

    Joined:
    Feb 7, 2017
    Posts:
    288
    If I use delayCall, it seems like the delayed call gets called twice when play mode starts. Avoided problem from that using a simple if else statement. But now I get random null reference exception due to delayCall. My uneducated guess is it's due to the instance of the script that calls for the delay call during editor mode not existing in play mode.

    I will check if it happens with different unity version and a fresh project.

    Edit: Yes. the error repeats
    Edit:fixed phrasing
     
    Last edited: Aug 29, 2019
  5. FerdowsurAsif

    FerdowsurAsif

    Joined:
    Feb 7, 2017
    Posts:
    288
    Warning is reproduced from this code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour
    6. {
    7.     void OnValidate()
    8.     {
    9.         GameObject obj = new GameObject();
    10.     }
    11. }
     
  6. Blinkq

    Blinkq

    Joined:
    Apr 19, 2015
    Posts:
    21
    yeap, have same error. This warning enoying, would appriciate, if someone will help with removing it
     
  7. ThatMunk

    ThatMunk

    Joined:
    May 17, 2017
    Posts:
    12
    +1 for a solution to this
    Running;
    Code (CSharp):
    1. Instantiate(Prefab, transform);
    on validate and getting this error in 2019.3.10f1.
     
  8. L42yB

    L42yB

    Joined:
    Dec 20, 2012
    Posts:
    34
    Bumping for a response. I have a ton of these warnings whenever I start the editor. I've seen no actual consequence for it, and I have no idea why they are happening.
     
  9. FerdowsurAsif

    FerdowsurAsif

    Joined:
    Feb 7, 2017
    Posts:
    288
    I fixed the warnings for my own code with the temporary fixes stated above. But,
    HDRP lighting system keeps spamming the warning at random times at random intervals.

    Unity version: 2019.4.1f1.

    Had the issue with previous versions for a LONG time.
    To be fair, I am not sure if I did something to cause this issue. I messed with HDRP settings a lot & didn't try reproducing it with a fresh new project.

    Exact message:
    Code (CSharp):
    1. SendMessage cannot be called during Awake, CheckConsistency, or OnValidate
    2. UnityEngine.Rendering.HighDefinition.HDAdditionalLightData:OnValidate()
    Got exactly 851 times this message after 5min of opening the project. All the messages came within 1min, then stopped. I just want to say, thank god Collapse similar log toggle exists. :D
     
    Last edited: Jul 30, 2020
  10. L42yB

    L42yB

    Joined:
    Dec 20, 2012
    Posts:
    34
    This is the full stack trace I get for mine in the log. Seems to be related to generating a preview for assets. I get hundreds of them too. I still don't know why or what is causing them...


    SendMessage cannot be called during Awake, CheckConsistency, or OnValidate
    UnityEditor.EditorUtility:Internal_InstantiateRemoveAllNonAnimationComponentsSingle_Injected(Object, Vector3&, Quaternion&)
    UnityEditor.EditorUtility:Internal_InstantiateRemoveAllNonAnimationComponentsSingle(Object, Vector3, Quaternion)
    UnityEditor.EditorUtility:InstantiateRemoveAllNonAnimationComponents(Object, Vector3, Quaternion)
    UnityEditor.EditorUtility:InstantiateForAnimatorPreview(Object)
    UnityEditor.PreviewData:UpdateGameObject(Object)
    UnityEditor.PreviewData:.ctor(Object)
    UnityEditor.GameObjectInspector:GetPreviewData()
    UnityEditor.GameObjectInspector:RenderStaticPreview(String, Object[], Int32, Int32)
    UnityEditor.AssetPreviewUpdater:CreatePreviewForAsset(Object, Object[], String)
     
  11. L42yB

    L42yB

    Joined:
    Dec 20, 2012
    Posts:
    34
    I also have this stack trace, which really looks like it is entirely a Unity issue:

    SendMessage cannot be called during Awake, CheckConsistency, or OnValidate
    UnityEditorInternal.InternalEditorUtility:LoadSerializedFileAndForget(String)
    UnityEditor.WindowLayout:LoadDefaultWindowPreferences()
     
  12. gcast1995

    gcast1995

    Joined:
    Oct 27, 2019
    Posts:
    25
    Bump, also having a similar issue.
     
  13. vsugrob

    vsugrob

    Joined:
    Mar 9, 2014
    Posts:
    5
    Bump. In my case it's reproduced by calling
    gameObject.AddComponent <AnyComponentType> ();
    from
    OnValidate()
    .
    Console output becomes unreadable.
     
  14. SunSailor

    SunSailor

    Joined:
    Aug 22, 2012
    Posts:
    26
    Actually, like in many other cases, Unity doesn't want us to do structural automatisms within OnValidate. Sadly, I don't know any other way to do such things, as in my case, I need to add a bunch of components, which act as proxies for to be generated ScriptableObjects.
     
  15. Masea1

    Masea1

    Joined:
    Jun 9, 2018
    Posts:
    41
    Same issue here.
     
  16. SpaceMoose9k

    SpaceMoose9k

    Joined:
    Jul 27, 2018
    Posts:
    5
    I have to wholesale turn off Warnings in the log because of this. There does not appear to be any solution. I wish we could just disable this specific warning somehow.
     
  17. HunterAhlquist

    HunterAhlquist

    Joined:
    Oct 6, 2013
    Posts:
    132
    I get this warning when running Resources.LoadAll