Custom Array

I get this error on line 41:

NullReferenceException: Object reference not set to an instance of an object
Main.Start () (at Assets/Main.cs:41)

Script:

using UnityEngine;
using System.Collections;

[System.Serializable]
public class _ID {
    public string DisplayName;
    public bool Transparent;
    public _ID() {
       
    }
}

public class Main : MonoBehaviour {

    //User input
    public int ChunkAmount;
    public int MaxHeight;
   
    //Setting up things to make this work.
    [HideInInspector] public int[,][,,] WorldSpace;//[Chunk_X,Chunk_Y][Block_X,Block_Y,Block_Z] = Value : Block ID [Note: The height goes one block higher than specified. This higher block will not render, and the value will be the height mapping.]
    [HideInInspector] public _ID[] ID;
   
    /*Block IDs [Note: Do not actually use the first 0's of 0-9. They are just for spacing.]
    00:Air
    01:Grass
    02:smile:irt
    03:
    04:
    05:
    06:
    07:
    08:
    09:
    10:
    */
   
    void Start () {
        WorldSpace = new int[ChunkAmount, ChunkAmount][,,];
       
        ID = new _ID[3];
        ID[0].DisplayName="Air";ID[0].Transparent=true;
        ID[1].DisplayName="Grass";ID[1].Transparent=false;
        ID[2].DisplayName="Dirt";ID[2].Transparent=false;
        Debug.Log(ID[1].DisplayName);
       
       
        for (int i = 0; i < ChunkAmount; i++) {
            for (int j = 0; j < ChunkAmount; j++) {
                WorldSpace[i,j] = new int[16,MaxHeight,16];
            }
        }
    }

    void Update () {
        //WorldSpace[0,0][10,10,10] = 5;
        //Debug.Log (WorldSpace[0,0][10,10,10]);
    }
}

Can anyone help me? :smile:
–Michael

You only have a new _ID[ ] array
The array is empty, you need to make new _ID’s for each element

Can you show me an example please?

ID = new _ID[3];

This line only initialises the array for use, it still doesn’t contain any data. i.e. it doesn’t contain any instances of the ID class, just the memory to store them.

You need to do something like:

for (int i = 0; i < ID.Length; i++)
{
     ID[i] = new _ID();
}

That would create an instance of the _ID class in each item of the array that you can assign to.

Thanks