Search Unity

Custom Array

Discussion in 'Scripting' started by TheNoobieWaffle, Dec 4, 2014.

  1. TheNoobieWaffle

    TheNoobieWaffle

    Joined:
    Jul 12, 2014
    Posts:
    25
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class _ID {
    6.     public string DisplayName;
    7.     public bool Transparent;
    8.     public _ID() {
    9.        
    10.     }
    11. }
    12.  
    13. public class Main : MonoBehaviour {
    14.  
    15.     //User input
    16.     public int ChunkAmount;
    17.     public int MaxHeight;
    18.    
    19.     //Setting up things to make this work.
    20.     [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.]
    21.     [HideInInspector] public _ID[] ID;
    22.    
    23.     /*Block IDs [Note: Do not actually use the first 0's of 0-9. They are just for spacing.]
    24.     00:Air
    25.     01:Grass
    26.     02:Dirt
    27.     03:
    28.     04:
    29.     05:
    30.     06:
    31.     07:
    32.     08:
    33.     09:
    34.     10:
    35.     */
    36.    
    37.     void Start () {
    38.         WorldSpace = new int[ChunkAmount, ChunkAmount][,,];
    39.        
    40.         ID = new _ID[3];
    41.         ID[0].DisplayName="Air";ID[0].Transparent=true;
    42.         ID[1].DisplayName="Grass";ID[1].Transparent=false;
    43.         ID[2].DisplayName="Dirt";ID[2].Transparent=false;
    44.         Debug.Log(ID[1].DisplayName);
    45.        
    46.        
    47.         for (int i = 0; i < ChunkAmount; i++) {
    48.             for (int j = 0; j < ChunkAmount; j++) {
    49.                 WorldSpace[i,j] = new int[16,MaxHeight,16];
    50.             }
    51.         }
    52.     }
    53.  
    54.     void Update () {
    55.         //WorldSpace[0,0][10,10,10] = 5;
    56.         //Debug.Log (WorldSpace[0,0][10,10,10]);
    57.     }
    58. }
    59.  
    Can anyone help me? :D
    --Michael
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,130
    You only have a new _ID[] array
    The array is empty, you need to make new _ID's for each element
     
  3. TheNoobieWaffle

    TheNoobieWaffle

    Joined:
    Jul 12, 2014
    Posts:
    25
    Can you show me an example please?
     
  4. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    Code (CSharp):
    1. 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:

    Code (CSharp):
    1. for (int i = 0; i < ID.Length; i++)
    2. {
    3.      ID[i] = new _ID();
    4. }
    That would create an instance of the _ID class in each item of the array that you can assign to.
     
  5. TheNoobieWaffle

    TheNoobieWaffle

    Joined:
    Jul 12, 2014
    Posts:
    25
unityunity