how i can access to subclasses variable by scripts

hello, i want to ask to how i can access to a nested class of a script and use the variable

some1.some2.some3 coordin; //here i'm declaring the script

coordin = gameobject.GetComponent<some1.some2.some3>(); //getting from the gameobject

and then use like this maxX = coordin.direction.x; //try to accessing at the variable

while the source script is something like that

public class some1 : MonoBehaviour {

//somethings

[System.Serializable]
        public class some2
        {
            [System.Serializable]
            public class some3
            {
                [SerializeField]
 public Vector3 direction=vector3.one; //i need this value

i can’t access it, unity give me an error saying it cannot access it from the gameobject or that “some2” or “some3” are not component… i don’t know how i can access to them…

Nested classes are types, not instances (objects).
If you want them to be instances you have to new them up and assign them to variables and then access those variables instead of the classes. Just like what you’d have to do if they weren’t nested classes

public class TestNest : MonoBehaviour
{
    public Nest1 nest1Instance;
    void Start()
    {
        nest1Instance = new Nest1();

        Debug.Log(this.nest1Instance.num);
        Debug.Log(this.nest1Instance.nest2Instance.num);
    }

    public class Nest1
    {
        public int num = 1;
        public Nest2 nest2Instance;
        public Nest1()
        {
            nest2Instance = new Nest2();
        }

        public class Nest2
        {
            public int num = 2;
            public Nest2()
            {

            }
        }
    }
}
1 Like

wow! it was pretty complicate, i followed your instruction and some other things and it worked, thanks