Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How do I stop the header attribute from repeating?

Discussion in 'Scripting' started by imaginereadingthis, Jan 27, 2023.

  1. imaginereadingthis

    imaginereadingthis

    Joined:
    Jul 25, 2020
    Posts:
    97
    Hello everyone,

    This is a dumb question, but how do I make it so that a header will only appear once instead of appearing on top of every variable that is related to it?
    upload_2023-1-26_18-23-53.png
    And I want it to look like this:

    Game Events
    Grab Object
    Release Object
     
  2. imaginereadingthis

    imaginereadingthis

    Joined:
    Jul 25, 2020
    Posts:
    97
    Oh, nevermind. I found the answer myself by accidentally changing how I declared the variables. Before, I declared them as

    public GameEvent grabObject, releaseObject;

    and now I changed it to

    public GameEvent grabObject;
    public GameEvent releaseObject;

    ༼ʘ̚ل͜ʘ̚༽ why does this happen??
     
  3. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    616
    Personally I don't think "why" is too important, something to do with how Unity parses that line. The clearer alternative from a programmer standpoint is not to list multiple properties but rather to separate them as you have done.
     
  4. imaginereadingthis

    imaginereadingthis

    Joined:
    Jul 25, 2020
    Posts:
    97
    yeah good point
     
  5. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Here's the simple answer: You don't.
    A header is related to one single variable. No more.
    Other variables after it are simple displayed below that variable.
    Thus, you only add the [Header()]-Attribute to whatever you want the header to display in front of (i.e. the 'top-most' variable in your script).

    Because when you declare multiple variables in a single line, all attributes above that line are applied to all variables in that line (seperately).
     
    Bunny83 likes this.
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,915
    I would disagree on that ^^.

    Nope, not related to Unity, that's my first point.

    It has to do how attributes are applied to members. When you put an attribute right before a declaration, it applies to the following declared member. When you group several members in one declaration, the attribute is applied to each member. This is useful when you want to do this for example:

    Code (CSharp):
    1. [System.Serializable]
    2. private Transform t1, t2, t3, t4;
    Here the Serializable attribute is applied to all 4 fields. The Header attribute is essentially just a hack Unity used to "insert" metadata in the code that the editor can interpret. The Header attribute is picked up by a built-in DecoratorDrawer which draws that header above the field it's attached to.

    So when you do

    Code (CSharp):
    1. [Header("Some transforms")]
    2. [System.Serializable]
    3. private Transform t1, t2, t3, t4;
    You're actually declaring this:

    Code (CSharp):
    1. [Header("Some transforms")]
    2. [System.Serializable]
    3. private Transform t1;
    4. [Header("Some transforms")]
    5. [System.Serializable]
    6. private Transform t2;
    7. [Header("Some transforms")]
    8. [System.Serializable]
    9. private Transform t3;
    10. [Header("Some transforms")]
    11. [System.Serializable]
    12. private Transform t4;
    Since all attributes you apply to a grouped declaration are applied to each member of that group.
     
  7. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    616
    A great explanation
     
  8. imaginereadingthis

    imaginereadingthis

    Joined:
    Jul 25, 2020
    Posts:
    97
    Thank you for this explanation!