Search Unity

Why is my GameObject in Class Array == null?

Discussion in 'Scripting' started by Chaosgod_Esper, Dec 18, 2014.

  1. Chaosgod_Esper

    Chaosgod_Esper

    Joined:
    Oct 25, 2012
    Posts:
    295
    hi there..
    I've written this small script:
    http://pastebin.com/WDeCkhPC

    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?
     
    Last edited: Dec 19, 2014
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Nah, you never create any "new BlockmapClass()"
    On top of that, you also never create any TopVertex = new int[x]; or GroundVertex = new int[x];
     
  3. Chaosgod_Esper

    Chaosgod_Esper

    Joined:
    Oct 25, 2012
    Posts:
    295
    i create new BlockmapClass in the CreateMap().
    the ints are created right before i fill them.
     
  4. NareshKhandla

    NareshKhandla

    Joined:
    Aug 6, 2013
    Posts:
    28
    please post your sample code from you geting error.
     
    Last edited: Dec 19, 2014
  5. Chaosgod_Esper

    Chaosgod_Esper

    Joined:
    Oct 25, 2012
    Posts:
    295
    The script is in the startpost...
    i updated the link to my version with Constructor.
     
  6. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    No, he's right, you never create an instance of BlockmapClass.. You just instantiate the size of your array.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class GameMap_Blockmap : MonoBehaviour {
    5.         public GameObject BasicBlock;
    6.         public Vector2 Mapsize = Vector2.zero;
    7.         private Mesh BasicMesh, CurrentMesh;
    8.         private Vector3 Meshsize, InstantPos;
    9.         private int ix,iy;
    10.         public class BlockmapClass{
    11.                 public GameObject Block;
    12.                 public int TileID;
    13.                 public int[] TopVertex;
    14.                 public int[] GroundVertex;
    15.                 public BlockmapClass(){
    16.                         Block = new GameObject("BasicBlock");
    17.                         TileID = 0;
    18.                         TopVertex = new int[4];
    19.                         GroundVertex = new int[4];
    20.                 }
    21.         }
    22.      
    23.         public BlockmapClass[][] Blockmap;
    24.      
    25.         // Use this for initialization
    26.         void Start () {
    27.                 CreateMap();
    28.         }
    29.         public void CreateMap(){
    30.                 StartCoroutine(CreateBlocks());
    31.         }
    32.         IEnumerator CreateBlocks(){
    33.                 BasicMesh = BasicBlock.GetComponent<MeshFilter>().sharedMesh;
    34.                 Meshsize = BasicMesh.bounds.size;
    35.                 Blockmap = new BlockmapClass[(int)Mapsize.y][];
    36.                 for(iy=0; iy<Mapsize.y; iy++){
    37.                         Blockmap[iy] = new BlockmapClass[(int)Mapsize.x];
    38.                 }
    39.                 yield return new WaitForEndOfFrame();
    40.                 for(iy=0; iy<Mapsize.y; iy++){
    41.                         for(ix=0; ix<Mapsize.x; ix++){
    42.                                 Debug.Log(iy + "/" + ix);
    43.                                 InstantPos.x = Mapsize.x*Meshsize.x;
    44.                                 InstantPos.y = 0;
    45.                                 InstantPos.z = Mapsize.y*Meshsize.z;
    46.  
    47. // HERE
    48. Blockmap[iy][ix] = new BlockmapClass();
    49.  
    50.  
    51.                                 Blockmap[iy][ix].Block = Instantiate(BasicBlock, InstantPos, Quaternion.Euler(Vector3.zero)) as GameObject;
    52.                              
    53.                                 Blockmap[iy][ix].TileID = 0;
    54.                              
    55.                                 CurrentMesh = Blockmap[iy][ix].Block.GetComponent<MeshFilter>().sharedMesh;
    56.                              
    57.                                 Blockmap[iy][ix].TopVertex[0] = CurrentMesh.triangles[0];
    58.                                 Blockmap[iy][ix].TopVertex[1] = CurrentMesh.triangles[1];
    59.                                 Blockmap[iy][ix].TopVertex[2] = CurrentMesh.triangles[2];
    60.                                 Blockmap[iy][ix].TopVertex[3] = CurrentMesh.triangles[3];
    61.                                 Blockmap[iy][ix].GroundVertex[0] = CurrentMesh.triangles[4];
    62.                                 Blockmap[iy][ix].GroundVertex[1] = CurrentMesh.triangles[5];
    63.                                 Blockmap[iy][ix].GroundVertex[2] = CurrentMesh.triangles[6];
    64.                                 Blockmap[iy][ix].GroundVertex[3] = CurrentMesh.triangles[7];
    65.                                 Blockmap[iy][ix].Block.transform.parent = transform;
    66.                         }
    67.                         yield return new WaitForEndOfFrame();
    68.                 }
    69.         }
    70. }
     
  7. Chaosgod_Esper

    Chaosgod_Esper

    Joined:
    Oct 25, 2012
    Posts:
    295
    ah - okay
    thank you all :)