- Home /
Putting user defined struct in Unity Editor
How can i make to apear public a struct defined by me in Unity Editor?
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;
...
}
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.)
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.
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?
@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[];
@$$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
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 () {
}
}
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.
Question and answer are here: http://answers.unity3d.com/questions/243464/problems-with-editing-a-class-from-the-editor.html
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.
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.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How does Unity retain UnityEngine.Object references? 2 Answers
Unity 64 bit color? 0 Answers
Unity stops after showing splash screen. 1 Answer
Custom Unity Editor Issue 1 Answer