Assigning an array values within an array

I’m feeling rather ineffectual at the moment - I’m attempting to grab a width value from a script attached to a GameObject using GetComponent for each item in an array of GameObjects.

Except - it likes to tell me the “Object reference not set to an instance of an object.”

    public GameObject[] chunks;
    private LevelChunk[] levelChunks;

    void Start () {
        for (int x = 0; x < chunks.Length; x++)
        {
            levelChunks[x] = chunks[x].GetComponent<LevelChunk>();
            print (levelChunks[x].width);
        }

What am I doing wrong here?
Thanks for any help.

You don’t seem to have initialized levelChunks anywhere, so it’s always just null.

–Eric

You need to add a line like this just before your for loop;

levelChunks = new LevelChunk[chunks.Length];

~
Another walk in the park.

Doh! Thanks for your help!

You’re welcome!

~
Another walk in the park.