Why is my GameObject in Class Array == null?

hi there…
I’ve written this small script:

This should create GameObjects inside a Subclass Array, and fill the Class values.

The Problem:
Inside the two loops, ‘Block’, ‘TileID’, ‘TopVertex’ and ‘GroundVertex’ are not existant…
as if the Class is empty!
Every value gives NullReference Errors.

I was looking over this code for the whole day now, but can’t find the Problem…

Can anyone guide me in the right direction, please?

Nah, you never create any “new BlockmapClass()”
On top of that, you also never create any TopVertex = new int[×]; or GroundVertex = new int[×];

i create new BlockmapClass in the CreateMap().
the ints are created right before i fill them.

please post your sample code from you geting error.

The script is in the startpost…
i updated the link to my version with Constructor.

No, he’s right, you never create an instance of BlockmapClass… You just instantiate the size of your array.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameMap_Blockmap : MonoBehaviour {
        public GameObject BasicBlock;
        public Vector2 Mapsize = Vector2.zero;
        private Mesh BasicMesh, CurrentMesh;
        private Vector3 Meshsize, InstantPos;
        private int ix,iy;
        public class BlockmapClass{
                public GameObject Block;
                public int TileID;
                public int[] TopVertex;
                public int[] GroundVertex;
                public BlockmapClass(){
                        Block = new GameObject("BasicBlock");
                        TileID = 0;
                        TopVertex = new int[4];
                        GroundVertex = new int[4];
                }
        }
     
        public BlockmapClass[][] Blockmap;
     
        // Use this for initialization
        void Start () {
                CreateMap();
        }
        public void CreateMap(){
                StartCoroutine(CreateBlocks());
        }
        IEnumerator CreateBlocks(){
                BasicMesh = BasicBlock.GetComponent<MeshFilter>().sharedMesh;
                Meshsize = BasicMesh.bounds.size;
                Blockmap = new BlockmapClass[(int)Mapsize.y][];
                for(iy=0; iy<Mapsize.y; iy++){
                        Blockmap[iy] = new BlockmapClass[(int)Mapsize.x];
                }
                yield return new WaitForEndOfFrame();
                for(iy=0; iy<Mapsize.y; iy++){
                        for(ix=0; ix<Mapsize.x; ix++){
                                Debug.Log(iy + "/" + ix);
                                InstantPos.x = Mapsize.x*Meshsize.x;
                                InstantPos.y = 0;
                                InstantPos.z = Mapsize.y*Meshsize.z;

// HERE
Blockmap[iy][ix] = new BlockmapClass();


                                Blockmap[iy][ix].Block = Instantiate(BasicBlock, InstantPos, Quaternion.Euler(Vector3.zero)) as GameObject;
                             
                                Blockmap[iy][ix].TileID = 0;
                             
                                CurrentMesh = Blockmap[iy][ix].Block.GetComponent<MeshFilter>().sharedMesh;
                             
                                Blockmap[iy][ix].TopVertex[0] = CurrentMesh.triangles[0];
                                Blockmap[iy][ix].TopVertex[1] = CurrentMesh.triangles[1];
                                Blockmap[iy][ix].TopVertex[2] = CurrentMesh.triangles[2];
                                Blockmap[iy][ix].TopVertex[3] = CurrentMesh.triangles[3];
                                Blockmap[iy][ix].GroundVertex[0] = CurrentMesh.triangles[4];
                                Blockmap[iy][ix].GroundVertex[1] = CurrentMesh.triangles[5];
                                Blockmap[iy][ix].GroundVertex[2] = CurrentMesh.triangles[6];
                                Blockmap[iy][ix].GroundVertex[3] = CurrentMesh.triangles[7];
                                Blockmap[iy][ix].Block.transform.parent = transform;
                        }
                        yield return new WaitForEndOfFrame();
                }
        }
}

ah - okay
thank you all :slight_smile: