• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 25, 2022 at 03:29 PM by Bunny83 for the following reason:

The question is answered, right answer was accepted

avatar image
Question by Golan2781 · Dec 28, 2012 at 10:53 PM · javascriptinspectorstruct

custom struct not showing up in inspector?

I'm trying to cluster data into better handleable chunks. I'm experimenting with classes and Javascript structs (as described here). My custom structs are not showing up in the inspector even though similarly coded classes do! I can access/edit them from a script just fine but tracking them in the inspector or even setting their values is not possible. What's wrong wit my implementation?

 #pragma strict
 
 var dummy1 : Vector3; // shows up
 var dummy2 : TestStruct; // does not show up
 var dummy3 : TestClass; // shows up
 var dummy4 : Vector3; // shows up
 
 function Start () {
     dummy2.t=2;
     dummy3.t=3;
     print(dummy2);
     print(dummy2.t);
     print(dummy3);
     print(dummy3.t);
 }
 
 class TestStruct extends System.ValueType{
     var x : float;
     var y : float;
     var z : float;
     var t : float;
 }
 class TestClass {
     var x : float;
     var y : float;
     var z : float;
     var t : float;
 }


Comment
BozontGames

People who like this

1 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by Setzer22 · Dec 28, 2012 at 11:14 PM

Update
Unity now supports the serialization of structs, so the old answer does no longer apply to the current versions of Unity. No need to downvote ancient answers that were perfectly valid when they were posted 10 years ago.


I'm afraid this can't be done on Unity. Classes will do fine but structs can't be serialized with Unity and thus, won't display on the inspector. I don't know the reasons for this limitation (I think it's fair to call it a limitation) but I bet this is because of structs being value types, not refference ones, but as floats and integers can be assigned, there might be something else there...

Anyway, you might want to check this out: http://forum.unity3d.com/threads/36395-How-do-I-make-member-structs-accessible-in-the-editor?highlight=struct+inspector

Comment
Golan2781
stevethorne
nonlin
Smaika

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Golan2781 · Dec 29, 2012 at 08:25 AM 0
Share

Thanks, that'll save me a lot of time trying. :D

It seems like a pretty hefty limitation indeed, especially seeing how Unity's own structs work fine and fast. Is there a feature request for this? I wasn't able to find one.

avatar image

Answer by jannis99 · Dec 11, 2015 at 03:23 PM

Serializing structs in Unity is actually possible now. I found this on the forums (Link):

     [System.Serializable]
     public struct PlayerStats
     {
         public int moveSpeed;
     }

I have tried it and it works fine.

Comment
stevethorne
hersheys72
Phorsaken
SuperS06
nonlin
Gelian
Zeratul3D
Cynikal
fffMalzbier
battlecompanytestee
Zack-Londres
GrooveJones
Lightning_A
Reid_Taylor
watcharayui
And 5 more...

People who like this

20 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zeratul3D · Jul 26, 2018 at 04:02 PM 1
Share

Works perfectly also on Unity 2018.

avatar image Zack-Londres · Aug 04, 2020 at 06:41 PM 1
Share

This is the way

avatar image RendergonPolygons · Jul 25, 2022 at 02:58 PM 0
Share

doesn't work for me U. 2020.3.37

full code

 public class WorldController : MonoBehaviour {
 
  public GameObject block;
  [System.Serializable]
  public struct WorldSize
  {
      public int x, y, z;//tried with [SerializeField] for these int but didn't do anything
      public WorldSize(int x, int y, int z)//removing the constructor didn't help show variables in inspector either
      {
          this.x = x;
          this.y = y;
          this.z = z;
      }
  }
  void Start()
  {
      StartCoroutine(BuildWorld());
  }
  IEnumerator BuildWorld()
  {
      WorldSize thisWorldSize = new WorldSize(10, 2, 10);
      for(int z=0;z< thisWorldSize.z; z++)
      {
          for (int y = 0; y < thisWorldSize.y; y++)
          {
              for (int x = 0; x < thisWorldSize.x; x++)
              {
                  Vector3 pos = new Vector3(x, y, z);
                  float r = Random.Range(0.0f, 1.0f);
                  if (r > 0.5 && (y>=(thisWorldSize.y-2)))
                      continue;
                  GameObject cube = GameObject.Instantiate(block, pos, Quaternion.identity);
                  cube.name = x + "_" + y + "_" + z;
              }
              yield return null;
          }
      }
  }
  // Update is called once per frame
  void Update()
  {
      
  }
 }
 
avatar image Bunny83 RendergonPolygons · Jul 25, 2022 at 03:26 PM 0
Share

Why do you again post on this ancient question? At the time this question as asked structs were not supported at all when it comes to serialization. That's why the selected answer was actually correct back then.


You should ask your own seperate question when you have a question. We can't really address different questions inside comments of other answers.


Anyways, your monobehaviour class does not have any field of your struct type. You only defined the struct type as a nested type but you don't have a serialized variable inside the monobehaviour. You only use it inside your coroutine in a local variable. You still haven't really said WHAT does not work. Again, stop posting here. Create your own question and add more details about your issue.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fields using Struct not showing up in Inspector. 1 Answer

Need health to decrease with movement in Javascript 1 Answer

Inspector Assigned Variables Not In Both Scenes 1 Answer

Change variable in inspector depending on enum using struct 0 Answers

Change a Class in the inspector 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges