Getting this error at runtime, the code that isnt working is commented. Not sure why it isnt working as I have instantiated the collider using ‘new’. Tried a few different things but no luck. Ignore the bad formatting if you can, it decided to do that after pasting in.
NullReferenceException
UnityEngine.BoxCollider.set_size (Vector3 value) (at C:/buildslave/unity/build/artifacts/generated/common/modules/DynamicsBindings.gen.cs:2282)
Chunk.SetSize (Vector3 newSize) (at Assets/Resources/MainScript.cs:13)
MainScript.Start () (at Assets/Resources/MainScript.cs:39)
using UnityEngine;
// The struct for a chunk, each chunk has a 3D array of tiles/cubes and a box collider
public struct Chunk
{
public GameObject[,,] tiles;
public BoxCollider collider;
}
// The main script to run the game
public class MainScript : MonoBehaviour
{
public const int CHUNKSIZE = 8; // Size of the chunks, XYZ
public const int MAPSIZE = 3; // Size of the map (in chunks) XZ
public const int MAPHEIGHT = 3; // Size of map (in chunks) Y
Chunk[,,] map = new Chunk[MAPSIZE, MAPHEIGHT, MAPSIZE]; // An array of chunks 2D array, the map
// Chunks being a struct containing a 3D array of tiles
// initialization
void Start()
{
// Loop through chunks then all tiles within a chunk and initialise them
for (int Z = 0; Z < MAPSIZE; Z++)
{
for (int Y = 0; Y < MAPHEIGHT; Y++)
{
for (int X = 0; X < MAPSIZE; X++)
{
map[X, Y, Z].tiles = new GameObject[CHUNKSIZE, CHUNKSIZE, CHUNKSIZE];
map[X, Y, Z].collider = new BoxCollider();
// Not working
map[X, Y, Z].collider.size = new Vector3(TileScript.SIZE * CHUNKSIZE, TileScript.SIZE * CHUNKSIZE, TileScript.SIZE * CHUNKSIZE);
map[X, Y, Z].collider.center = new Vector3((X * TileScript.SIZE * CHUNKSIZE) + (CHUNKSIZE / 2),
(Y * TileScript.SIZE * CHUNKSIZE) + (CHUNKSIZE / 2),
(Z * TileScript.SIZE * CHUNKSIZE) + (CHUNKSIZE / 2));
// End of not working code
for (int z = 0; z < CHUNKSIZE; z++)
{
for (int y = 0; y < CHUNKSIZE; y++)
{
for (int x = 0; x < CHUNKSIZE; x++)
{
map[X, Y, Z].tiles[x, y, z] = Instantiate(Resources.Load("Tile/Tile"), new Vector3(
(x * TileScript.SIZE) + (X * TileScript.SIZE * CHUNKSIZE),
(y * TileScript.SIZE) + (Y * TileScript.SIZE * CHUNKSIZE),
(z * TileScript.SIZE) + (Z * TileScript.SIZE * CHUNKSIZE)),
new Quaternion(0, 0, 0, 0)) as GameObject;
TileScript temp = map[X, Y, Z].tiles[x, y, z].GetComponent<TileScript>();
if (y < CHUNKSIZE / 2)
{
temp.SetType(Type.AIR);
}
else
{
temp.SetType(Type.OTHER);
}
} // x
} // y
} // z
} // X
} // Y
} // Z
}
// Update function
void Update()
{
} // Update
}