Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

What is the best way to get reference of parent gameobject's script component?

Discussion in 'Scripting' started by dealexce, Apr 1, 2022.

  1. dealexce

    dealexce

    Joined:
    Aug 31, 2020
    Posts:
    3
    Hi, I'm working on a project in Unity3D. In my project, the script components on child game objects often need to keep an instance reference of script component on their parent game object. I find that there are several ways to get the parent script instance, but I don't know which one is preferred, and I want to make it consistent in my project.
    For instance, the scene structure is:

    Factory (FactoryController.cs)
    -Machine (MachineController.cs)
    -Worker (WorkerController.cs)
    -...
    Factory (FactoryController.cs)
    -Machine (MachineController.cs)
    -Worker (WorkerController.cs)
    -...

    The MachineController and WorkerController each has a variable refers to parent controller:
    Code (CSharp):
    1. public FactoryController factoryController;
    To get the reference of the instance of script on each's parent, I can do this in Start() in child controllers:
    Code (CSharp):
    1. ...
    2. factoryController = GetComponentInParent<FactoryController>();
    3. ...
    The other way is to do this in Start() of the parent controller script:
    Code (CSharp):
    1. ...
    2. MachineController mc = GetComponentInChildren<MachineController>();
    3. mc.planeController = this;
    4. ...
    I'm wondering is there a preferred way for this? Thanks!
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    1.- Why dont you assing it manually and create a Prefab out of it?
    2.- I would suggest, as "if" the Machine and the Worker have the Factory assigned already,
    then create a "Subscribe()" function for the Machine and the Worker in the Factory which you call on "Start()".

    I would highly suggest to automate your system as much as possible "yes" but also assign whatever you need in the Inspector before you Start, that way you will save computing capacity and your Garbage Collector will that you for that as well.

    Small Edit:

    i would not suggest using this here as well:
    Code (CSharp):
    1. factoryController = GetComponentInParent<FactoryController>();
    Just assign it in the Inspector, easy as it is :)
     
    dealexce and Bunny83 like this.
  3. dealexce

    dealexce

    Joined:
    Aug 31, 2020
    Posts:
    3
    Thanks a lot for your suggestion!
    So GetComponentInParent and GetComponentInChildren are inefficient. But another reason I want in-script assigning is that the instances of Machine and Worker are instantiated during runtime and there may be multiple instances and the number is not fixed.
    So I guess the compromised approaches are the second one, or leave a manually assigned Machine/Worker prefab in Factory and clone it every time I want to instantiate a new instance?
     
  4. Omniglitch

    Omniglitch

    Joined:
    May 29, 2017
    Posts:
    37
    If your Factory will always have 1 Machine and 1 Worker, and you're always instantiating the same thing, then it's best to set that up in a prefab, and assign the reference through the inspector in the prefab editor. Even if you end up needing a handful of different types of Factories, you can make more prefabs (or prefab variants). Only if you need an unwieldy number of Factory types, or have some sort of procedural/random Factory generator would you need to assign the references at runtime through code.
     
    dealexce and Kurt-Dekker like this.