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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to display a struct field in inspector?

Discussion in 'Scripting' started by the_gnoblin, Oct 11, 2010.

  1. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    Hello!

    I have problems while trying to show

    Code (csharp):
    1. public Rect2 rect;
    in inspector,
    where Rect2 is a struct:

    Code (csharp):
    1. [System.Serializable]
    2. public struct Rect2
    3. {
    4.     public bool a;
    5.     public int b;
    6.     public int c;
    7.  
    8. }
    If I make it a class, then everything works ok:

    Code (csharp):
    1. [System.Serializable]
    2. public class Rect2
    3. {
    4.     public bool a;
    5.     public int b;
    6.     public int c;
    7. }
    Is it possible to show a struct in inspector? Vector3, Rect, Vector2 are all structs and we see them in the inspector, so I am a little bit confused.

    Thank you!
     
    SaSha_K likes this.
  2. Prodigalpops

    Prodigalpops

    Joined:
    May 5, 2010
    Posts:
    86
  3. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    That other thread pretty much sums it up. In short, you're out of luck :(

    I seem to be one of the few who thinks being able to serialize custom structs in Unity would be useful. But, I would guess there's a valid technical reason it's not supported.

    As for Unity's own struct types, I imagine those are just handled as special cases.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    For the record, since this is the first Google result that comes up when you search for it: this works now.

    You do have to use System.Serializable and make sure your struct members are public, but assuming you do that, it works fine, both for single members and for arrays of them.
     
    kemijo, Melanzue, Songersoft and 25 others like this.
  5. browne11

    browne11

    Joined:
    Apr 30, 2015
    Posts:
    128
    Pretty old post but I can confirm the above is true. Thanks Joe!

    [System.Serializable] right above your declared struct.
     
  6. aranthel09

    aranthel09

    Joined:
    Jul 17, 2015
    Posts:
    66
    This still doesn't work for me. I have this:
    Code (CSharp):
    1. [System.Serializable]
    2.     public struct EquippedItem
    3.     {
    4.         public long id;
    5.         public string type;
    6.         public string name;
    7.         public string equipmentSlot;
    8.     }
    however, nothing shows in the inspector anyway
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    It will work as long as you have a field of the type EquippedItem exposed to the inspector. Normally via a MonoBehaviour or a ScriptableObject.
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    It should. Please show the MonoBehaviour where you're using this.

    (Doh! Sniped by @BoredMormon!)
     
    Kiwasi likes this.
  9. aranthel09

    aranthel09

    Joined:
    Jul 17, 2015
    Posts:
    66
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4. using UnityEngine.UI;
    5. using System.Linq;
    6.  
    7. public class PartySystem : NetworkBehaviour {
    8.  
    9.  
    10.     [SyncVar]public string partyName = "";
    11.     [SyncVar]public int partyLevel = 0;
    12.     [SyncVar]public float partyEXP = 0f;
    13.     [SyncVar]public int maxMembers = 5;
    14.     [SyncVar]public string partyLeader = "";
    15.  
    16.     [System.Serializable]
    17.     public struct EquippedItem
    18.     {
    19.         public long id;
    20.         public string type;
    21.         public string name;
    22.         public string equipmentSlot;
    23.     }
    That's my script
     
    Hiddensquid_ likes this.
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You need to add public EquippedItem equippedItem; to your variable declarations.
     
  11. jjhunt

    jjhunt

    Joined:
    Sep 26, 2014
    Posts:
    1
    thanks
     
  12. WilliamBerne

    WilliamBerne

    Joined:
    Jul 17, 2012
    Posts:
    2
    Thanks, it worked!
     
  13. adityasreenivas

    adityasreenivas

    Joined:
    Sep 13, 2017
    Posts:
    6
    Hello Kiwasi! Can we use [System.Serializable] to display variables kept inside a nested structure.....
     
  14. rajanerve

    rajanerve

    Joined:
    Nov 17, 2017
    Posts:
    17
    Hai! I had tried [System.Serializable] for simple structure with few variables. But that is not listing up in the Inspector. Could anyone please help!! The structure is kept within monobehaviour .
     
  15. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Please post the code (use the Insert Code button, located between the movie filmstrip and the floppy disk icon in the toolbar, to format it correctly). Show both where the struct is declared, and the MonoBehaviour where you are using it.
     
  16. rajanerve

    rajanerve

    Joined:
    Nov 17, 2017
    Posts:
    17
    Code (CSharp):
    1. using System;
    2. using System.Net;
    3. using System.Net.Sockets;
    4. using System.IO;
    5. using System.Runtime.Interopservices;
    6. using UnityEngine;
    7. using System.Threading;
    8. public class NetworkServer:MonoBehaviour
    9. {
    10. public int portAddress;
    11. public int AltHold,AP_Engage;
    12. SampleData sData = new SampleData();
    13. [System.Serializable]
    14. public struct tempControls
    15. {
    16. public float gama;
    17. public float track;
    18. }
    19. [System.Serializable]
    20. public struct Controls
    21. {
    22. public float gama_ref;
    23. public float track_error;
    24. public tempcontrols tc;
    25. }
    26.  
    That's my script
     
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You would have to have a variable of type 'tempControls' and 'Controls' to see them in the inspector. Maybe just 'Controls' in your case, and you'd see that plus the tempcontrols member. :)
     
    unity_Sh5lfZokN7wC_Q likes this.
  18. rajanerve

    rajanerve

    Joined:
    Nov 17, 2017
    Posts:
    17
    :) Thank u.Can you explain me with an code snipppet.
     
  19. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    a code snippet of making a variable?
    Code (csharp):
    1. public Controls myControls; // wow! :)
    try that.
     
  20. rajanerve

    rajanerve

    Joined:
    Nov 17, 2017
    Posts:
    17
    Thanks a lot :).Finally it worked:).
    still I have a doubt whether it is mandatory to make my class as Monobehaviour.
     
  21. adityasreenivas

    adityasreenivas

    Joined:
    Sep 13, 2017
    Posts:
    6
    Hai! I had tried listing up variables of a structure in an inspector.,I succeeded,Thanks a lot methos5k and kiwasi. But i wish to configure a particular set of gameobjects(switches) to do particular animations depending upon structure variables , which i wish to configure through inspector.

    Below SwitchTypeis is an enum which tells the switch type. Refer the snap attached, through which switch type will be configured. Similarly, I wish Onswitch,Offswitch,Ontoidleswitch,Offtoidleswitch,OntoOffswitch and OfftoOnswitch to be a structure variable which i wish to list up in the inspector just like enum-dropdown.

    Current These variables "Onswitch.... OfftoOnswitch" were declared public keycodes to make the action.

    Code (CSharp):
    1. switch (SwitchTypis)
    2.         {
    3.         /*******************************OnOffType****************************************************/
    4.         case SwitchType.OnOffType:
    5.             if (Input.GetKey (Onswitch))
    6.                 //Anim2.Play ("OnOffType");
    7.                 anim.SetBool ("OnOffStatus", true);
    8.  
    9.             if (Input.GetKey (Offswitch))
    10.                 //Anim2.Rewind ();
    11.                 anim.SetBool ("OnOffStatus", false);
    12.             break;
    13.             /*********************************PullOnOffType**************************************************/
    14.         case SwitchType.PullOnOffType:
    15.             if (Input.GetKey (Onswitch))
    16.                 //Anim2.Play ("OnOffType");
    17.                 anim.SetBool ("OnOffStatus", true);
    18.  
    19.             if (Input.GetKey (Offswitch))
    20.                 //Anim2.Rewind ();
    21.                 anim.SetBool ("OnOffStatus", false);
    22.             break;
    23.             /**********************************OnIdleOffType*************************************************/
    24.         case SwitchType.OnIdleOffType:
    25.             if (Input.GetKey (Onswitch))
    26.                 anim.SetInteger ("Switchpos", 1);
    27.  
    28.             if (Input.GetKey (Offswitch))
    29.                 anim.SetInteger ("Switchpos", 2);
    30.  
    31.             if (Input.GetKey (Offtoidleswitch))
    32.                 anim.SetInteger ("Switchpos", 3);
    33.  
    34.             if (Input.GetKey (Ontoidleswitch))
    35.                 anim.SetInteger ("Switchpos", 5);
    36.  
    37.             if (Input.GetKey (Ontooffswitch))
    38.                 anim.SetInteger ("Switchpos", 4);
    39.  
    40.             if (Input.GetKey (Offtoonswitch))
    41.                 anim.SetInteger ("Switchpos", 6);
    42.             break;
    43.             /***********************************PullOnIdleOffType************************************************/
    44.         case SwitchType.PullOnIdleOffType:
    45.             if (Input.GetKey (Onswitch))
    46.                 anim.SetInteger ("Switchpos", 1);
    47.  
    48.             if (Input.GetKey (Offswitch))
    49.                 anim.SetInteger ("Switchpos", 2);
    50.  
    51.             if (Input.GetKey (Offtoidleswitch))
    52.                 anim.SetInteger ("Switchpos", 3);
    53.  
    54.             if (Input.GetKey (Ontoidleswitch))
    55.                 anim.SetInteger ("Switchpos", 5);
    56.  
    57.             if (Input.GetKey (Ontooffswitch))
    58.                 anim.SetInteger ("Switchpos", 4);
    59.  
    60.             if (Input.GetKey (Offtoonswitch))
    61.                 anim.SetInteger ("Switchpos", 6);
    62.             break;
    63.             /***************************************SpringLoadedOnOff********************************************/
    64.         case SwitchType.SpringLoadedOnOff:
    65.             if (Input.GetKey (Onswitch))
    66.                 //Anim2.Play ("OnOffType");
    67.                 anim.SetBool ("Springloadon", true);
    68.  
    69.             if (Input.GetKey (Offswitch))
    70.                 //Anim2.Rewind ();
    71.                 anim.SetBool ("Springloadon", false);
    72.             break;
    73.             /***********************************SpringLoadedOnIdleOff************************************************/
    74.         case SwitchType.SpringLoadedOnIdleOff:
    75.             if (Input.GetKey (Onswitch))
    76.                 anim.SetInteger ("Springload", 1);
    77.  
    78.             if (Input.GetKey (Offswitch))
    79.                 anim.SetInteger ("Springload", 2);
    80.  
    81.             if (Input.GetKey (Offtoidleswitch))
    82.                 anim.SetInteger ("Springload", 3);
    83.  
    84.             if (Input.GetKey (Ontoidleswitch))
    85.                 anim.SetInteger ("Springload", 4);
    86.  
    87.             break;
    88.  
    89.         default:
     

    Attached Files:

  22. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    @adityasreenivas, that is getting well off the topic of this (old) thread. Please start a new thread with your question about animating based on a public enum or integer.
     
  23. SaSha_K

    SaSha_K

    Joined:
    Jan 4, 2017
    Posts:
    7
    Putting all together:
    * struct should be marked by [Serializable]
    * and it should be declared as public with public fields
     
    Genebris and ComradeVanti like this.
  24. PashDev

    PashDev

    Joined:
    Nov 6, 2021
    Posts:
    2
    for the record this will do fine:

    Code (CSharp):
    1. public class Exmple : MonoBehaviour
    2. {
    3.         public Data D = new Data{};
    4. }
    5.  
    6. [System.Serializable]
    7. public struct Data
    8. {
    9.     public uint Var;
    10. }