How to access gameobject from sub-class?

Sorry I ask this question, but I’ve searched for answer 10 hours long with no success.

public class BigClass : MonoBehaviour {
  public GameObject BASE1;
  public GameObject BASE2;
  public GameObject AnOtherObjectToAccess;
  int someValue;
// ...

  public class SubPropertiesClass : MonoBehaviour {
    int otherValueSpecificToBase;
// ...
    public void ReArrange() {
         this.gameObject.transform.parent = AnOtherObjectToAccess // << ... ERROR
    }
  }

void Start() {
// ...
   BASE1.AddComponent<SubPropertiesClass>();
   BASE2.AddComponent<SubPropertiesClass>();
// ...
}

}

transform.parent is another Transform, not a GameObject.So you need to:

gameObject.transform.parent = AnOtherObjectToAccess.transform;

Also note that “this” is redundant.

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'.

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.

Typically you’d hand the references in immediately after AddComponent.

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

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.

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.)