Issue with a for loop.
Hello, I have been experimenting with Dijkstra's path finding. I have one for loop that goes through each node and checks its y+1 position to the position of the next node in the list to see if it is above it, if it is, it creates a connection by storing the the second node in a list of connections in the first node. This works fine. The next section of the code does the exact same thing however it looks for y-1 to see if there is a node below the current node in the list. This for loop is not working. Using the debug console I can see that the for loop is triggered, however the checkDown() method returns false 100% of the time and I am unsure why. Any help would be appreciated, code below.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
/// /// The Graph. /// public class Graph : MonoBehaviour {
[SerializeField]
public List<Node> m_Nodes = new List<Node> ();
[SerializeField]
public Node newNode;
private BoardCreator board;
private int nodeListSize;
int nextnameNumber = 0;
public virtual List<Node> nodes
{
get
{
return m_Nodes;
}
}
void Start(){
board = GameObject.FindObjectOfType<BoardCreator> ();
setUpNodes ();
setupConnections();
}
public void setUpNodes(){
//the two nested for loops will loop through every tile in BoardCreator
for (int i = 0; i < board.tiles.Length; i++) {
for (int j = 0; j < board.tiles [i].Length; j++) {
if (board.floorCheck (i, j) == true) {//if the position in x,y returns true in the floorCheck
Vector3 newNodePos = new Vector3 (i, j, 0);//set the nodes position to the true x,y coords
Node newNodeInstance = Instantiate (newNode, newNodePos, Quaternion.identity) as Node; //instantiate as a node
newNodeInstance.name = "Node" + nextnameNumber;//give the instantiated node a unique name
m_Nodes.Add (newNodeInstance);//add the new node to the list of nodes
newNodeInstance.transform.parent = this.transform;//set the node to be a child of the graph
nextnameNumber++;//increase the number added to the end of the node name by 1 every iteration
}
}
}
}
public void setupConnections(){
Debug.Log ("I made it to up connections!");
nodeListSize = m_Nodes.Count;
//this works to get the dynamic size of the node list when the graph is called to be created
//this will be used in conjunction with the code below to establish connections between nodes
//m_Nodes[0].connections.Add(m_Nodes[3]);
//m_Nodes[0].connections.Add(m_Nodes[2]);
//the above also works
for (int i = 0; i < nodeListSize - 1; i++) {
if (checkAbove (m_Nodes [i], m_Nodes [i+1]) == true) {
m_Nodes [i].connections.Add (m_Nodes [i+1]);
Debug.Log ("I added an up connection");
}
}
for (int x = 0; x < nodeListSize - 1; x++) {
if (checkBelow (m_Nodes [x], m_Nodes [x+1]) == true) {
m_Nodes [x].connections.Add (m_Nodes [x+1]);
Debug.Log ("I added a down connection");
}
}
}
public bool checkAbove(Node thisNode, Node nextNode){
bool isAbove;
if (Mathf.Approximately(thisNode.transform.position.y + 1, nextNode.transform.position.y)) {
Debug.Log ("checkAbove is true");
isAbove = true;
return isAbove;
} else {
Debug.Log ("checkAbove is false");
isAbove = false;
return isAbove;
}
}
public bool checkBelow(Node thisNode, Node nextNode){
bool isBelow;
if (Mathf.Approximately(thisNode.transform.position.y - 1, nextNode.transform.position.y)) {
Debug.Log ("checkBelow is true");
isBelow = true;
return isBelow;
} else {
Debug.Log ("checkBelow is false");
isBelow = false;
return isBelow;
}
}
Your answer

Follow this Question
Related Questions
Spawn unique objects from the array 1 Answer
Array help with targeting system 0 Answers
How do i make Unity seamlessly loop my background music? 5 Answers