Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by TheJokersWild09 · Apr 13, 2018 at 11:59 PM · looplists

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;
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawn unique objects from the array 1 Answer

Problem with For Loop for Text objects,Why isn't this for loop for a list of text objects not working? 0 Answers

Array help with targeting system 0 Answers

How to deal with a missing reference when an object referred each frame from an array is destroyed along the way? 2 Answers

How do i make Unity seamlessly loop my background music? 5 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges