Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 /
avatar image
2
Question by ZsurzsaLaszlo · Mar 09, 2011 at 11:34 PM · editorstruct

Putting user defined struct in Unity Editor

How can i make to apear public a struct defined by me in Unity Editor?

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by yoyo · Mar 09, 2011 at 11:43 PM

EDIT: Unity serialization does not support structs. You can create and serialize your own types, but they must be classes, not structs.

Mark your class as serializable and then create a member of that type on a component. Now when you add the component to a game object and select it in the hierarchy, your data will be editable in the inspector.

For example (C#) ...

public class MyComponent : MonoBehaviour { // Declare your serializable data. [System.Serializable] public class CoffeeMaker { public float WaterCapacity; public float BrewTime; public string Manufacturer; }

 // Create a component field using your custom type.
 public CoffeeMaker CoffeePot;

 ...

}

Comment
Add comment · Show 6 · 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 Jesse Anders · Mar 10, 2011 at 05:18 AM 0
Share

Are you sure that works? Have you tried it? ($$anonymous$$y understanding - and experience - is that struct types cannot be made serializable, in previous versions of Unity at least.)

avatar image yoyo · Mar 10, 2011 at 05:53 AM 0
Share

Sorry, you are totally correct. Structs don't work, but classes do -- edited accordingly (and actually tested this time!! ;) Structs and classes are logically very similar, the main difference is that a struct is a value type. Using classes should do the trick for you, unless there is some special reason you need a struct.

avatar image manavkataria · Apr 16, 2012 at 09:50 PM 0
Share

This was really helpful! However, I am not sure why but I am unable declare an array of CoffeePot. I looked up Unity documentation but did not find anything useful apart from help on Serializable. Any tips?

avatar image Bunny83 · Apr 16, 2012 at 10:25 PM 0
Share

@manavkataria: I guess you mean an array of Coffee$$anonymous$$aker since Coffee$$anonymous$$aker is the class ;)

 //C#
 public Coffee$$anonymous$$aker[] CoffeePots;
 
 //UnityScript
 var CoffeePots : Coffee$$anonymous$$aker[];
avatar image Lorodion · Dec 13, 2013 at 12:24 PM 1
Share

@$$anonymous$$ryptos No, member variables should start with an uppercase letter (if they are public):

√ DO use PascalCasing for all public member, type, and namespace names consisting of multiple words.

http://msdn.microsoft.com/en-us/library/ms229043(v=vs.110).aspx

Show more comments
avatar image
0

Answer by Danta1st · Apr 22, 2012 at 11:09 PM

i've tried this method, and it does give me the ability to assign values in the editor. Or rather... it seems like it does. What actually happens is that it nullifies those values when i start the emulation/script and sets them all to zero. Any suggestions? using UnityEngine; using System.Collections; //using System.Serializable;

public class turbines : MonoBehaviour {

 [System.Serializable]
 public class turbine{
     public string model;
     public int lifetime;
     public float height;
     public int ratedWind;
     public int cutinWind;
     public int cutoutWind;
 }
 /*
 public struct turbine{
     public string model;
     public int lifetime;
     public float height;
     public int ratedWind;
     public int cutinWind;
     public int cutoutWind;
 }*/
 public turbine V10018MW;
 
 // Use this for initialization
 void Start () {
     V10018MW = new turbine();
     Debug.Log(V10018MW);
 }
 
 // Update is called once per frame
 void Update () {
 
 }

}

Comment
Add comment · Show 2 · 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 syclamoth · Apr 22, 2012 at 11:52 PM 0
Share

You shouldn't have posted this as an answer. Delete this post and repost as a question, and I'll tell you what you're doing wrong.

avatar image MrBorogove · Apr 07, 2014 at 04:57 PM 0
Share

Question and answer are here: http://answers.unity3d.com/questions/243464/problems-with-editing-a-class-from-the-editor.html

avatar image
0

Answer by Zarenityx · Jan 20, 2013 at 02:37 PM

In C# there is a struct keyword, but a good way to make structs is to make a class that inherits from System.Object. In C# you will need to use [System.Serializable]. In JavaScript it does it automatically. If you use something like this: (I normally use JS so my C# syntax might be a bit off... Sorry!)

 #pragma strict
 //JavaScript
 class MyStruct extends System.Object{
      var MyFloat : float;
      var MyString : String;
 }
 //C#
 class MyStruct{
      public float MyFloat;
      public String MyString;
 }

Then you can make variables of type MyStruct, and access the variables MyFloat and MyString.

Comment
Add comment · 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
0

Answer by Gamestroyer · Mar 28, 2021 at 10:21 AM

 [System.Serializable]
 public struct Stage
 {
       public float requiredMass;
       public Material mat;
 }
 
 public Stage stage;

This method works for me, Use [System.Serializable] on declaration of struct instead of the variable.

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

How does Unity retain UnityEngine.Object references? 2 Answers

Unity 64 bit color? 0 Answers

Custom Unity Editor Issue 1 Answer

Paint on object in editor and return as texture. 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