I’m having some trouble trying to maintain a referenceto my Controller class from each of my other classes. I’m trying to maintain this reference by calling GetComponentInParent
during the Awake
method, however this returns null. If I call it during the Start
method, it returns as would be expected.
Say I have two objects in my Scene:
-
Controller (parent)
-
Player (child)
public class Controller : MonoBehaviour
{
private Player _player;
private void Awake()
{
// Gets the object as expected
_player = GetComponentInChildren<Player>();
}
}
public class Player : MonoBehaviour
{
private Controller _controller;
private void Awake()
{
// fails to get the object, returns null
_controller = GetComponentInParent<Controller>();
}
private void Start()
{
// gets the object
if (_controller == null)
_controller = GetComponentInParent<Controller>();
}
}
During the Player.Awake()
call, the Controller
component cannot be found, and returns null. However, in the Player.Start()
call, it’s found.
I really want to link up my dependencies during the Awake
function for all classes - how can I achieve this?
Thanks!
Note: My script execution order dictates that the Controller script will run before the Player script, and this is visually confirmed while the debugger is attached to Unity.
I have done this for you. Different methods to access script of parent object from child object vice versa. [Use of GetComponentsInChildren() and GetComponentInParent()]
3 Scripts each holding a method that I am accessing : GrandParent , Parent, Child attached to 3 Objects
- GrandParentObject
- --------------------------- ParentObject
- ------------------------------------------------- ParentObject
This Script Shows multiple methods of doing the same thing for better understanding. There is another way of doing it by just making a public static instance of a class and then u can use that instance directly in other class without needing to create an object of that class. I can go in detail of this one in comments if required. Thanks if it answers what you are looking for please mark it as answered. Cheers!
GrandParent Class
using UnityEngine;
public class GrandParent : MonoBehaviour {
//Method 1: Accessing Child Script From GrandParent Script
GameObject parentObject;
GameObject childObject;
Child child;
Parent parent;
//Method 2: Accessing Child Script From GrandParent Scrip
public Transform[] childrenObjects;
void Awake () {
print("---------------- METHOD 1 -------------");
parentObject = transform.Find("Parent").gameObject;
childObject = transform.Find("Parent/Child").gameObject;
parent = parentObject.GetComponent<Parent>();
child = childObject.GetComponent<Child>();
child.InChild();
parent.InParent();
print(" --------- Method 1 short to access child -------------");
child = this.transform.Find("Parent/Child").gameObject.GetComponent<Child>();
child.InChild();
//METHOD 2
print("----- Method 2 GetComponentsInChildren When you have Alot of childrens -----");
childrenObjects = GetComponentsInChildren<Transform>();
for(int i = 0; i < childrenObjects.Length; i++)
{
if (childrenObjects*.name == "Child")*
{
child = childrenObjects*.GetComponent();*
child.InChild();
}
if (childrenObjects*.name == “Parent”)*
{
parent = childrenObjects*.GetComponent();*
parent.InParent();
}
}
print(“----- METHOD 2 With FOREACH Loop ----”);
foreach(Transform children in childrenObjects)
{
if(children.Find(“Child”)){
child = children.GetComponentInChildren();
child.InChild();
}
if(children.Find(“Parent”)){
parent = children.GetComponentInChildren();
parent.InParent();
}
}
}
public void InGrandParent()
{
print(“InGrandParent Object”);
*} *
}
----------
Child Class
----------
using UnityEngine;
public class Child : MonoBehaviour {
Transform GrandParentObject;
GrandParent gp;
Parent p;
public void Start()
{
print(“--------- METHOD 1: From Child T GRAND PARENT ---------”);
GrandParentObject = transform.root;
gp = GrandParentObject.GetComponent();
gp.InGrandParent();
print(“-----------METHOD 2: From Child to Parents ----------------”);
gp = GetComponentInParent();
gp.InGrandParent();
p = GetComponentInParent();
p.InParent();
}
public void InChild()
{
Debug.Log(“child component found”);
}
}