Nested classes in C# not showing up in inspector?

Hi

I’m writing a C# script, and nested classes/structs are not showing up there.

I’ll explain in code.

This javascript code (NestedClassJS.js) :

class TestNested
{
    var a : int = 1;
};

var nestedInstance : TestNested;

function Update () {

}

Causes ‘nestedInstance’ to appear in the inspector tab when the script is applied to a gameobject.

However, this C# code (NestedClassCS.cs) :

using UnityEngine;

public class TestNestedCS
{
    int B;
};

public class NestedClassCS : MonoBehaviour {

    public TestNestedCS testNestedInstance;

	// Update is called once per frame
	void Update () {
	
	}
}

Does not. I tried using structs instead of classes, placing the nested class inside and outside the class definition and still nothing.

Does anyone know what I need to do to get the nested instance to appear in the inspector?

Thanks!

I think you have to mark the classes with a [ Serializable ] attribute, but I’m not sure:

[ Serializable]
public class TestNestedCS
{
    int B;
};

You’ll need to add the Serializable attribute to the class. This attribute is added by default in JS, but not in CS.

using UnityEngine;

[System.Serializable]
public class TestNestedCS
{
    int B;
};

public class NestedClassCS : MonoBehaviour {

    public TestNestedCS testNestedInstance;

	// Update is called once per frame
	void Update () {
	
	}
}

Thanks for the confirmation (and full namespace/code), tomvds; glad to know I’m not always talking out of my ass. :smile: