Linking GameObjects together in start, link's disappear after finished.

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!

    for(int i = 0; i < nodesList.Capacity; ++i)

you should not iterate over capacity but over count. the memory is reallocated in certain steps when adding values to avoid reallocation and copying at every add. so capacity is almost always larger than count and when you iterate over empty objects your program may behave unexpected.

        case Directions.south:

        {

            arrayOfNodes[(int)direction] = node;

            Node otherNodeScript = node.GetComponent<Node>() as Node;

            otherNodeScript.arrayOfNodes[(int)Directions.north] = this.gameObject;

        }

afaik the curly braces are not needed as the block is confined by case and break.

to your problem its hard to tell from the given code. how are the nodes created? what else happens in start? why do you think they are not connected? you should post the whole script (or at least the relevant parts).

Ok I’ll try to clarify some more. I have a node prefab that I give to my graph. My graph instantiates clones based on that prefab and sent gives them the proper position and size.

for(int i = 0; i < amountOfNodesInX; ++i)
		{
			for(int k = 0; k < amountOfNodesInZ; ++k)
			{
				Vector3 position = targetBoxPosition;
				
				GameObject nodeClone = Instantiate(nodePrefab) as GameObject;
				
				if(!nodeClone.collider)
				{
					nodeClone.AddComponent<BoxCollider>();
				}
				
				position.x += (nodeClone.collider.bounds.size.x * i);
				position.z += (nodeClone.collider.bounds.size.z * k);
				
				nodeClone.transform.position = position;
				
				Node nodeScript = nodeClone.GetComponent<Node>() as Node;
				
				nodeScript.GetCube().renderer.material.color = new Color(Random.Range(0.00f, 0.255f), Random.Range(0.00f, 0.255f), Random.Range(0.00f, 0.255f));
				
				nodeScript.SetGraphPosition(i, k);
				
				graphList.Add(nodeScript.graphPosition, nodeClone);
					
				nodesList.Add(nodeClone);
				nodesScriptsList[(nodesList.Count - 1)] = nodeScript;
			}
		}

This is the start of Node.cs. I have since updated and tried using Dictionary instead of a list but I still have the problem

void Start () 
	{	
		graphPosition = Vector2.zero;
		
		gameObject.AddComponent<BoxCollider>();
		
		gameObject.GetComponent<BoxCollider>().isTrigger = true;
		
		if(isVisible)
		{
			renderer.enabled = true;
		}
		else
		{
			renderer.enabled = false;
		}
		
		nodesDictionary = new Dictionary<Directions, GameObject>(numberOfDirections);
		
		arrayOfNodes = new GameObject[numberOfDirections];
		
		for(int i = 0; i < numberOfDirections; ++i)
		{
			this.arrayOfNodes[i] = null;
		}
	}

I know they are not linked because once the game is loaded up and I click on each node clone and check the dictionaries/lists all GameObjects in them are set to None.