Search Unity

How to access gameobject from sub-class?

Discussion in 'Scripting' started by PizzaProgram, Apr 29, 2017.

  1. PizzaProgram

    PizzaProgram

    Joined:
    Feb 12, 2014
    Posts:
    17
    Sorry I ask this question, but I've searched for answer 10 hours long with no success.

    Code (CSharp):
    1. public class BigClass : MonoBehaviour {
    2.   public GameObject BASE1;
    3.   public GameObject BASE2;
    4.   public GameObject AnOtherObjectToAccess;
    5.   int someValue;
    6. // ...
    7.  
    8.   public class SubPropertiesClass : MonoBehaviour {
    9.     int otherValueSpecificToBase;
    10. // ...
    11.     public void ReArrange() {
    12.          this.gameObject.transform.parent = AnOtherObjectToAccess // << ... ERROR
    13.     }
    14.   }
    15.  
    16. void Start() {
    17. // ...
    18.    BASE1.AddComponent<SubPropertiesClass>();
    19.    BASE2.AddComponent<SubPropertiesClass>();
    20. // ...
    21. }
    22.  
    23. }
     
    Last edited: Apr 29, 2017
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    transform.parent is another Transform, not a GameObject.So you need to:

    Code (csharp):
    1. gameObject.transform.parent = AnOtherObjectToAccess.transform;
    Also note that "this" is redundant.
     
  3. PizzaProgram

    PizzaProgram

    Joined:
    Feb 12, 2014
    Posts:
    17
    Thank you for your answer. Yes, I know about ".transform", but the problem is, that AnOtherObjectToAccess is not accessible at ALL.
    Error CS0120 An object reference is required for the non-static field, method, or property 'BigClass.AnOtherObjectToAccess'.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I remember the topic of this post confusing me when I read it and saw the contents of the thread. Your definition of subclass, I mean.
    Do you mean a nested class? I haven't ever seen someone use a nested monobehaviour that I recall.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Typically you'd hand the references in immediately after AddComponent.

    In vanilla C# you use a constructor, but that's not allowed with MonoBehaviours.
     
  6. LovesSolvingProblems

    LovesSolvingProblems

    Joined:
    Jan 22, 2015
    Posts:
    17
    I'm not sure exactly what you are asking but your code looks more like composition than inheritance (inheritance has base and child classes)

    Composition combines smaller objects to make bigger objects (ie a car object has 4 wheels, a body, a door) In your example Bigclass is the car and Base1, Base2, and anotherobjecttoAccess are the wheels, door, body.

    In your example both Bigclass and SubPropertiesClass are subclassing Monobehavior

    Perhaps that would help you with your search or if you want to give some more detail I'll try and help you further.
     
  7. PizzaProgram

    PizzaProgram

    Joined:
    Feb 12, 2014
    Posts:
    17
    Thank you ALL for the help!
    - I know about inheritance. This is not what I'm looking for.
    - I would like to "extend" Base1 + Base2 with methods and properties during Runtime.
    ... staying with the example: the "car" (bigClass) has "multiple windows" with similar properties and functions. (Let's say they "break" and "repair" ... and the both affect other properties of the car.)