• 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 /
avatar image
0
Question by Atrus_Madhatter · Jan 31, 2013 at 07:19 PM · c#foreachoopnode

[C#] foreach, lists and objects :D

Okay, here we go... I have a list of classes: I used {} because it thinks that those are the HTML tags so it doesn't show the <>

 List{Node} nodeList = new List{Node}();

And once I press the button I add new Start Node:

 if(Input.GetKeyUp(KeyCode.P)){
     // first argument is IDCounter so that every node has unique ID;
     nodeList.Add(new StartNode(++nodeCounter, 10, 10));
 }

So far, so good. Then I put some code to add another type of node. Now, every node has overrided method NodeLogic which has Debug.Log(NodeID); which is just counter. And in order for me to run this method I used foreach loop...

 foreach (Node node in nodeList) {
     node.NodeLogic();
 }

but on Log its quite interesting:

 --:Break:--
 Start1
 --:Break:--
 Start2
 Start2
 --:Break:--
 Start3
 Start3
 Start3
 --:Break:--
 Start4
 Start4
 Start4
 Start4

When it should be:

 --:Break:--
 Start1
 --:Break:--
 Start1
 Start2
 --:Break:--
 Start1
 Start2
 Start3

What it seems like that it counts how many objects there are in list and print out the ID of the last added Node that number of time. Or is it how Objects work.

EDIT: I added some more code.

     //------Class that holds all the variables
     //List Holding Nodes
     List<Node> nodeList = new List<Node>();
     //Id Counter
     private int nodeCounter = 0;

 // StartNode Class

 using UnityEngine;
 using System.Collections;
 
 public class StartNode : Node {
     public StartNode(int IDcounter, float x, float y):base(IDcounter, x, y, "Start", 0, 1, true){
     }
     public override void NodeLogic(){
         Debug.Log(NodeID);
     }
     public override void BaseNodeDraw ()
     {
         base.BaseNodeDraw ();
     }
 }
 //---------This is parent constructor
 public Node(int IDcounter, float x, float y, string NodeN, int NumInCh, int NumOutCh, bool activ){
         NodeName = NodeN;
         InputChannel = new string[NumInCh];
         OutputChannel = new string[NumOutCh];
         xPos = x;
         yPos = y;
         nodeWindow = new Rect(x,y,50,50);
         activated = activ;
         NodeID = string.Format("{0}{1}",NodeName,IDcounter);
     }
Comment
Add comment · Show 6
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
avatar image PAEvenson · Jan 31, 2013 at 07:24 PM 2
Share

how about you show us your NodeLogic function

avatar image Julien-Lynge · Jan 31, 2013 at 10:16 PM 1
Share

Yeah, how have you defined a 'Node'?

avatar image Wolfram · Jan 31, 2013 at 10:35 PM 1
Share

We need to see the complete code for NodeLogic, the definition of nodeID, and the code for the StartNode constructor, before this question can be answered without wild guessing.

avatar image Wolfram · Feb 01, 2013 at 12:04 PM 1
Share

What about the declaration of NodeID? Where is it, and is it static, as @$$anonymous$$ suggests?

avatar image Atrus_Madhatter · Feb 01, 2013 at 12:33 PM 0
Share

NodeID is just a string as you can see:

 public string NodeID;

And then i declare it in base Node class with:

 NodeID = string.Format("{0}{1}",NodeName,IDcounter);
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by RudyTheDev · Jan 31, 2013 at 10:30 PM

You probably have a "static" keyword added to whatever variable holds the node's index.

Comment
Add comment · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

RPGStatSystem tutorial problem with clases visibility? 1 Answer

Question on Using a Foreach Loop on Nested Children 1 Answer


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