Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question how i can access to subclasses variable by scripts

Discussion in 'Scripting' started by Parapin, Sep 10, 2023.

  1. Parapin

    Parapin

    Joined:
    Mar 31, 2017
    Posts:
    14
    hello, i want to ask to how i can access to a nested class of a script and use the variable
    Code (CSharp):
    1. some1.some2.some3 coordin; //here i'm declaring the script
    2.  
    3. coordin = gameobject.GetComponent<some1.some2.some3>(); //getting from the gameobject
    and then use like this
    Code (CSharp):
    1. maxX = coordin.direction.x; //try to accessing at the variable
    while the source script is something like that

    Code (CSharp):
    1. public class some1 : MonoBehaviour {
    2.  
    3. //somethings
    4.  
    5. [System.Serializable]
    6.         public class some2
    7.         {
    8.             [System.Serializable]
    9.             public class some3
    10.             {
    11.                 [SerializeField]
    12.  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..
     
  2. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,306
    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

    Code (CSharp):
    1.  
    2. public class TestNest : MonoBehaviour
    3. {
    4.     public Nest1 nest1Instance;
    5.     void Start()
    6.     {
    7.         nest1Instance = new Nest1();
    8.  
    9.         Debug.Log(this.nest1Instance.num);
    10.         Debug.Log(this.nest1Instance.nest2Instance.num);
    11.     }
    12.  
    13.     public class Nest1
    14.     {
    15.         public int num = 1;
    16.         public Nest2 nest2Instance;
    17.         public Nest1()
    18.         {
    19.             nest2Instance = new Nest2();
    20.         }
    21.  
    22.         public class Nest2
    23.         {
    24.             public int num = 2;
    25.             public Nest2()
    26.             {
    27.  
    28.             }
    29.         }
    30.     }
    31. }
    32.  
     
    Last edited: Sep 10, 2023
    Parapin likes this.
  3. Parapin

    Parapin

    Joined:
    Mar 31, 2017
    Posts:
    14
    wow! it was pretty complicate, i followed your instruction and some other things and it worked, thanks