Search Unity

Resolved Set tag/layer in OnValidate

Discussion in 'Scripting Dev Blitz Day 2023 - Q&A' started by Baste, Feb 22, 2023.

  1. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    I often want to set the tag and/or layers of objects in OnValidate:

    Code (csharp):
    1. private void OnValidate() {
    2.     gameObject.tag = "EditorOnly";
    3.     gameObject.layer = LayerMask.NameToLayer("Ignore Raycast");
    4. }
    I can do that just fine, but I get this warning:
    upload_2023-2-22_17-27-6.png

    Again, this code works. The layer and tag are both set correctly. But I get a warning that's a) completely wrong about what I'm doing (I'm not calling SendMessage) and b) unnecessary the code can very much be called!


    I've reported this as a bug before, and I've been told that it's intended behaviour. Any chance that this strangeness could be fixed?
     
    mariandev likes this.
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Hi,

    Do you have a bug number?
    I can see that we do send an event when this value is changed however this event is internal and there should be ways for us to do this without the warning. If you have the bug I can try and reopen it.
     
  3. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    You're not considering that perhaps Unity itself is using a SendMessage at some point in the setting of the layer, tag management... I have encountered this as well, and generally ignored it, as it was a warning, but i suspect that indeed unity is the entity using the send message at that time.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    I think it's a fogbugz era bug, and I don't have a link there available right now. I could make a new one?
     
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Yes that would help. Thanks.