C#: nested class and inspector ?

Hello,

Im noob with unity 3d and C#, but I’m long term user of c++. (this is why I want to use C# instead of JScript )

My problem is that I’m trying write some clean code and separate different data groups and want to use nested classes for data (Class declaration is inside scope of another class) … something like :

public class Class : MonoBehaviour 
{
	public class NestedClass
	{
             public float data;
        };

        public NestedClass data; 
}

When I add this script to some gameobject, I don’t see data inside inspector… but when its written in JScrit, i can see them ? Could you help me, what I’m doing wrong ?

Thank you very much…

Sanjuro…

I’m not sure but why would you add a ‘;’ after the closing bracket of the nested class? Old C++ habits? :wink:

Does this work:

public class Class : MonoBehaviour
{
   public class NestedClass : MonoBehaviour
   {
             public float data;
        }

        public NestedClass data;
}

:?:

hehe, yup… but it didn’t help when I removed :frowning: Oh and you added MonoBehaviour to Nested Class… Now it looks like nested class is added to inspector, but looks empty… Anyway, I hope, that nested class will not be inherited from MonoBehaviour class, so it will contain only necessary data…

Also I forget init data… so code look this :

public class Class : MonoBehaviour 
{ 
   public class NestedClass : MonoBehaviour 
   { 
             public float data; 
   } 

   public NestedClass data = new NestedClass() ; 
};

You need to add the Serializable attribute tag to the nested class(es), and you will want to remove the MonoBehaviour inheritance from it. The Serializable tag is in the System namespace.

public class Class : MonoBehaviour 
{ 
   [System.Serializable]
   public class NestedClass
   { 
             public float data; 
   } 

   public NestedClass data = new NestedClass() ; 
};

-Jeremy

1 Like

ah, I was searching for serialize… :roll:

thanks pal…

Sanjuro