Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

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.

enter image description here

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?

share|improve this question
2  
I didn't know Unity had a such GUI/IDE... Seems a bit... Intrusive. –  akled Jul 7 '12 at 19:44
    
did you try this method? GetComponentsInChildren<HasParallelogram>() –  Ali.S Jul 7 '12 at 21:17
    
@Gajet I considered it, but that returns an array, and I'd much rather do the loop to keep things in a list. Ultimately, I don't think this is likely to be the issue, as the issue isn't in finding the component. –  Raven Dreamer Jul 7 '12 at 21:25
    
Post updated. I found out why I was getting a null pointer. Still looking for a solution, however. –  Raven Dreamer Jul 7 '12 at 21:31
    
Some sort of coroutine, perhaps? –  Wackidev Jul 7 '12 at 21:39
add comment

1 Answer

up vote 2 down vote accepted

What is the exact error? All objects that you declare via the IDE should exist at the time that Awake is called. (http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Awake.html) So it should not be the case that the HasParallelagram script has not been instantiated. However, Start() will not have been called yet, and that's to be expected - Awake calls run before Start calls, and you can use that knowledge to ensure that internal data is set up in Awake before you perform operations in Start.

share|improve this answer
    
Oh, well that'd be the problem, then. I had thought it the reverse. Is there also an "Enable" function for when the object becomes enabled? –  Raven Dreamer Jul 8 '12 at 0:01
    
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.