So I’m trying to create a graph full of nodes. Each node is connected to 8 other nodes representing directions on a compass. When I’m debugging it says the nodes are connecting just fine. However after Start() is finished, in game the nodes are not connected to each other at all.
Here’s how I link them together.
for(int i = 0; i < nodesList.Capacity; ++i)
{
Node nodeScript = nodesList[i].GetComponent<Node>() as Node;
if(nodeScript.GetGraphPosition().y > 0)
{
Vector2 previousNodeKey = new Vector2(nodeScript.GetGraphPosition().x, nodeScript.GetGraphPosition().y - 1);
nodeScript.AddNodeToDirection(Directions.east, graphList.Find(previousNodeKey));
}
if(nodeScript.GetGraphPosition().x > 0)
{
if(nodeScript.GetGraphPosition().y > 0)
{
Vector2 southEastKey = new Vector2((nodeScript.GetGraphPosition().x - 1),(nodeScript.GetGraphPosition().y - 1));
nodeScript.AddNodeToDirection(Directions.southEast, graphList.Find(southEastKey));
Vector2 southKey = new Vector2((nodeScript.GetGraphPosition().x - 1),(nodeScript.GetGraphPosition().y));
nodeScript.AddNodeToDirection(Directions.south,graphList.Find(southKey));
if(nodeScript.GetGraphPosition().y < (amountOfNodesInZ - 1))
{
Vector2 southWestKey = new Vector2((nodeScript.GetGraphPosition().x - 1),(nodeScript.GetGraphPosition().y + 1));
nodeScript.AddNodeToDirection(Directions.southWest, graphList.Find(southWestKey));
}
}
else
{
Vector2 southKey = new Vector2((nodeScript.GetGraphPosition().x - 1), (nodeScript.GetGraphPosition().y));
nodeScript.AddNodeToDirection(Directions.south, graphList.Find(southKey));
if(nodeScript.GetGraphPosition().y < (amountOfNodesInZ - 1))
{
Vector2 southWestKey = new Vector2((nodeScript.GetGraphPosition().x - 1),(nodeScript.GetGraphPosition().y + 1));
nodeScript.AddNodeToDirection(Directions.south, graphList.Find(southWestKey));
}
}
}
}
AddNodeToDirection function:
public void AddNodeToDirection(Directions direction, GameObject node)
{
switch(direction)
{
case Directions.east:
{
this.arrayOfNodes[(int)direction] = node;
Node otherNodeScript = node.GetComponent<Node>() as Node;
otherNodeScript.arrayOfNodes[(int)Directions.west] = this.gameObject;
}
break;
case Directions.west:
{
this.arrayOfNodes[(int)direction] = node;
Node otherNodeScript = node.GetComponent<Node>() as Node;
otherNodeScript.arrayOfNodes[(int)Directions.east] = this.gameObject;
}
break;
case Directions.north:
{
this.arrayOfNodes[(int)direction] = node;
Node otherNodeScript = node.GetComponent<Node>() as Node;
otherNodeScript.arrayOfNodes[(int)Directions.south] = this.gameObject;
}
break;
case Directions.northEast:
{
this.arrayOfNodes[(int)direction] = node;
Node otherNodeScript = node.GetComponent<Node>() as Node;
otherNodeScript.arrayOfNodes[(int)Directions.southWest] = this.gameObject;
}
break;
case Directions.northWest:
{
this.arrayOfNodes[(int)direction] = node;
Node otherNodeScript = node.GetComponent<Node>() as Node;
otherNodeScript.arrayOfNodes[(int)Directions.southEast] = this.gameObject;
}
break;
case Directions.southEast:
{
arrayOfNodes[(int)direction] = node;
Node otherNodeScript = node.GetComponent<Node>() as Node;
otherNodeScript.arrayOfNodes[(int)Directions.northWest] = this.gameObject;
}
break;
case Directions.southWest:
{
arrayOfNodes[(int)direction] = node;
Node otherNodeScript = node.GetComponent<Node>() as Node;
otherNodeScript.arrayOfNodes[(int)Directions.northEast] = this.gameObject;
}
break;
case Directions.south:
{
arrayOfNodes[(int)direction] = node;
Node otherNodeScript = node.GetComponent<Node>() as Node;
otherNodeScript.arrayOfNodes[(int)Directions.north] = this.gameObject;
}
break;
}
}
There there is anything else that needs to be provided, just let me know!