What's a viable way to get public properties from child objects?

Posted by Raven Dreamer on Game Development See other posts from Game Development or by Raven Dreamer
Published on 2012-07-07T19:41:34Z Indexed on 2012/07/07 21:24 UTC
Read the original article Hit count: 407

Filed under:

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:

enter image description here

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");
        }
    }
}

Unfortunately, attempting to assign tempRect = tempsc.myRect causes a null pointer at run time.

Am I missing some crucial step? HasParallelgram is an empty script with a public Rect set in the editor and nothing else.

enter image description here

What's the proper way to get a child's component?

© Game Development or respective owner

Related posts about unity