I have a GameObject (RoomOrganizer in the picture below) with a "RoomManager" script, and one or more child objects, each with a 'HasParallelagram' component attached, likeso:
I've also got the following in the aforementioned "RoomManager"
void Awake ()
{
Rect tempRect;
HasParallelogram tempsc;
foreach (Transform child in transform)
{
try
{
tempsc = child.GetComponent<HasParallelogram>();
tempRect = tempsc.myRect;
blockedZoneList.Add(new Parallelogram(tempRect));
Debug.Log(tempRect.ToString());
}
catch( System.NullReferenceException)
{
Debug.Log("Null Reference Caught");
}
}
}
HasParallelgram is an empty script with a public Rect set in the editor and nothing else.
Unfortunately, attempting to assign tempRect = tempsc.myRect
causes a null pointer at run time.
I've done some more digging, and the reason for the null pointer is relatively simple - the RoomManager script gets woken ("Awake") before the HasParallelagram script gets instantiated ("Start").
It's an issue of timing - I need to wait until all objects have been instantiated before calling the RoomManager. Does Unity support a means of doing that?
What's the proper way to get a child's component?
GetComponentsInChildren<HasParallelogram>()
\$\endgroup\$