Hey guys, this is probably a stupid question but I don’t understand what’s going on.
I have a struct that I am saving in a dictionary and I have some component data in it, transform, meshrenderer, etc.
// LOAD: injects a chunk gameobject into the scene heirarchy
public void Load(ref Chunk chunk)
{
chunk.loaded = true;
chunk.transform = new GameObject("Chunk").transform;
chunk.transform.parent = _.game.grid.transform;
chunk.transform.position = new(chunk.position.x, chunk.position.y, chunk.position.z);
chunk.filter = chunk.transform.AddComponent<MeshFilter>();
chunk.renderer = chunk.transform.AddComponent<MeshRenderer>();
chunk.collider = chunk.transform.AddComponent<MeshCollider>();
chunk.filter.mesh = new Mesh();
chunk.renderer.material = Assets.Material("node");
chunk.collider.sharedMesh = chunk.filter.mesh;
}
And I am grabbing the chunks like so.
// GETCHUNK: returns a reference to a chunk based on its position
public static bool GetChunk(int3 position, out Chunk chunk)
{
if (_.game.grid.chunks.ContainsKey(position)) {
chunk = _.game.grid.chunks[position];
return true;
} else {
chunk = new Chunk();
return false;
}
}
So the problem is when I grab the struct from the dictionary and change some values.
_.GetChunk(position, out Chunk c);
c.Load(ref c);
Debug.Log(c.filter);
Debug.Log(c.nodes.Count());
c.nodes.Add(222, new Node());
_.GetChunk(position, out Chunk c1);
Debug.Log(c1.filter);
Debug.Log(c1.nodes.Count());
The node count gets updated but the component data does not. Just returns null when I try grabbing it the second time.
Sorry for the stupid question… I’m just not sure what’s going on. Structs are allowed to hold component data right?