0

I have a class with a default constructor , but when I create an array of this class I get a NullReferenceException

using UnityEngine;
using System.Collections;

public class Casilla {
    int _nCasilla { get; set; }
    bool _ocupada { get; set; }
    bool _blanca { get; set; }
    Vector3 posicion;

    public int nCasilla
    {
        get { return _nCasilla; }
        set { _nCasilla = value; }
    }
    public bool ocupada
    {
        get { return _ocupada; }
        set { _ocupada = value; }
    }

    public bool blanca
    {
        get { return _blanca; }
        set { _blanca = value; }
    }

    public Vector3 getPosicion()
    {
        return posicion;
    }
    public void setPosicion(float x, float z){
        posicion.x = x;
        posicion.y = 0;
        posicion.z = z;
    } 

    public Casilla(){
        posicion = new Vector3();
        ocupada = false;
        _blanca = true;
        posicion.x = 0;
        posicion.y = 0;
        posicion.z = 0;
    }
}

public class Tablero  {

    Casilla[,] tablero;
    bool blanca = true;

    public Tablero(){
        tablero = new Casilla[8,8];
        float x, z;
        x = z = 0;

        for (int i=0; i <8; i++){
            for (int j = 0; j < 8; j++){
                tablero[i, j].nCasilla = 8 * i + j;//The exception is here
                tablero[i, j].blanca = blanca;
                tablero[i, j].setPosicion(x, z);
                blanca = !blanca;
                x += 10;
            }
            x = 0;
            z -= 10;
        }
    }
}

I have small experience with C# but when you create a n array in C++ every element of this array is created calling the default constructor of Casilla class has a default constructor. So what is the problem??

StepUp
  • 27,357
  • 12
  • 66
  • 120
AFS
  • 1,363
  • 3
  • 25
  • 47

0 Answers0