When I'm creating a cube object in my class and trying to use my CreateVertexBuffer and CreateIndicesBuffer, I'm getting the error "object reference not set to an instance of an object". Even tho I'm creating the cube the right way, why is this happening?
public class Cube
{
public GraphicsDevice device;
const int number_of_vertices = 8;
public const int number_of_indices = 36;
public VertexBuffer vertices;
public IndexBuffer indices;
public Cube(GraphicsDevice graphicsDevice)
{
device = graphicsDevice;
}
public void CreateCubeVertexBuffer()
{
VertexPositionTexture[] cubeVertices = new VertexPositionTexture[number_of_vertices];
"VERTICES HERE"
vertices = new VertexBuffer(device, VertexPositionTexture.VertexDeclaration, number_of_vertices, BufferUsage.WriteOnly);
vertices.SetData<VertexPositionTexture>(cubeVertices);
}
public void CreateCubeIndexBuffer()
{
UInt16[] cubeIndices = new UInt16[number_of_indices];
"INDICES HERE"
indices = new IndexBuffer(device, IndexElementSize.SixteenBits, number_of_indices, BufferUsage.WriteOnly);
indices.SetData<UInt16>(cubeIndices);
}
public void Draw(BasicEffect effect)
{
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
effect.CurrentTechnique.Passes[0].Apply();
device.SetVertexBuffer(vertices);
device.Indices = indices;
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, number_of_vertices, 0, number_of_indices / 3);
}
}
}
}
The error is at the line indices = new IndexBuffer(blah, blah, blah);