Search Unity

How do I expose a C# class to the inspector?

Discussion in 'Scripting' started by Factoid, Apr 23, 2008.

  1. Factoid

    Factoid

    Joined:
    Mar 30, 2008
    Posts:
    69
    Hi there,

    When coding in JS, I'm able to do something like the following.

    Code (csharp):
    1.  
    2. class SomeClass
    3. {
    4.    var text : String;
    5.    var value : float;
    6.    var mat : Material;
    7. }
    8.  
    9. var classArray : SomeClass[];
    10.  
    At which point I can have an editable array of complex data types in the inspector.

    I'm having problems getting the same thing to work in C#.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PropData
    6. {
    7.     public string name;
    8.     public float value;
    9. }
    10.  
    11. public class Prop : MonoBehaviour {
    12.     public PropData propData = new PropData();
    13. }
    14.  
    I'd like to have this expose a complex type so that I can edit the values as a package (that can be passed around to other objects). In this trivial case it would be easier to simply have a name and value member variable in the Prop class, but that's not what I'm shooting for.

    This should be easy, I'm just missing something.
     
  2. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    I haven't tried what you describe before, but my instinct would be to use a struct rather than a class.
     
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Are you having problems because you are defining multiple classes, as only one of them can match the name of the script? If not, what kind of errors are being generated?
     
  4. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    try:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [System.Serializable]  // You need to have this line in there
    6. public class PropData
    7. {
    8.    public string name;
    9.    public float value;
    10. }
    11.  
    12. public class Prop : MonoBehaviour {
    13.    public PropData propData = new PropData();
    14. }
    15.  
     
    Smireles likes this.
  5. Factoid

    Factoid

    Joined:
    Mar 30, 2008
    Posts:
    69
    No errors are generated, the property simply doesn't show up in the inspector.

    If I make the PropData class extend Object, then it becomes viewable, but only as a 'socket' to attach an instance of the object, and not as an inspectable complex data type.
     
  6. Factoid

    Factoid

    Joined:
    Mar 30, 2008
    Posts:
    69
    Perfect! Problem solved!
     
  7. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    /me slaps forehead

    Ofcourse! Nicely done Nicholas :)
     
  8. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    And I now have something else to look up and learn about (Serializable)! More thanks!