- Home /
Defining a public class in C#
Hi, I am starting to really get my hands deep in regards of C#, but I really don't get how you define a class in C#(a class that is view-able in the Inspector)
In regards of JavaScript you define a class like this:
class className{
//Whatever
}
var CN : className; // This is what I dont get in C#
(That example is view-able and editable in the inspector)
In C# I currently got:
public class className{
//Whatever
}
(This class won't show in the Inspector)
My question is; how can I make a class(in C#) show in the editor? I looked at the Microsoft C# reference, but it didn't help me that much.
Any help would be much appreciated.
Answer by syclamoth · Jan 25, 2012 at 02:50 PM
Simple- you need to tell the compiler that a given class should be able to be shown in the editor using the 'System.Serializable' attribute. In this case, use it like this-
[System.Serializable]
public class Foo
{
public string text;
public int number;
public float value;
}
Thank, you. It really seams legit, and I checked the Scripting Reference, but it wont "Serialize" or show the class in the editor. Even when just copying and pasting you script(or the one from the Scripting reference) it still won't show. I also tried [SerializeField], but that wont show the class either. Any help would be very appreciated.
If your class only contains data and does not need to be attached to a gameobject, you can also inherit from ScriptableObject, which does essentially the same thing as the [System.Serializable] tag. Also, to get it to show in the inspector you need to create a variable for it. In Syclamoth's example, in your other class, create it like so:
public foo fooInstance;
Your answer
Follow this Question
Related Questions
Field is never assigned to and will always have its default value. 0 Answers
Field is never assigned to, and will always have its default value 3 Answers
How to make classes/structs public to all scripts 0 Answers
an object reference is required to access non static member problem 0 Answers
Multiple Cars not working 1 Answer