So here’s my issue:
I have a Planet class and a HexTile classes that I am using to create hexagonal (and 12 pentagonal) tiles on the surface of a sphere. My problem is coming from telling which tiles who their neighbors are.
My tiles are stored in List < HexTile > hexTiles
and each tile holds its own neighbors in List < int > neighbors
as an index to the hexTiles list. For some reason, when I ran my algorithm for finding each tile’s neighbors (confirmed not the issue) I got errors everywhere. I’ve narrowed it down to this most basic case.
Planet.cs
public class Planet : MonoBehaviour {
public List < GameObject > hexTiles;
void Start () {
hexTiles = new List < GameObject > ();
// Icosahedron code...
foreach ( Vector3 v in mesh.vertices ) {
GameObject temp = new GameObject ();
temp.AddComponent < HexTile > ();
hexTiles.Add ( temp );
}
// as a test case, trying to set the first neighbor of the first tile to 42
hexTiles [ 0 ].GetComponent < HexTile > ().neighbors.Add ( 42 ); // NullReferenceException Error
}
}
HexTile.cs
public class HexTile : MonoBehaviour {
public List < int > neighbors;
void Start () {
neighbors = new List < int > ();
neighbors.Add ( 42 ); // works fine here
}
}
Everything I’ve searched online has been people forgetting to initialize their List, but that I can edit the list from HexTile disproves that. If anyone has any info that could help, or know what I’m doing wrong, I would be very grateful for it!
If I need to add more info I can.
Thank you for reading!