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 /
This question was closed Mar 29, 2017 at 11:55 AM by cursedj07 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by cursedj07 · Mar 28, 2017 at 01:22 PM · c#listforeach

C# Foreach element[,] in list problem

Hello, so Im working on a BoardGame, and I would to do an actions (Damage them in this case) to all the piece/servant (they have the script Servant attached to them) that has a certain condition (all the ally in this case) on the board, so I have FindAllAlly () that finds all the ally servants, but I cant figure out how to use the list (how to do an actions to all the Servants[i,j] in the list. Here is the script that I tried to use :

 public Servant[,] Servants{ set; get; } 
 
 public List FindAllAlly () {

     List<Servant[,]> allyServants = new List<Servant[,]> ();
     for (int i = 0; i < tileNum; i++) {
         for (int j = 0; j < tileNum; j++) {
             if (Servants [i, j] != null) {        // si il y a un personnage
                 if (Servants [i, j].isPlayer == GameManager.Instance.isPlayerTurn) {
                     allyServants.Add(Servants [i, j]);
                 }
             }
         }
     }
     return allyServants;
 }

 private void TestServant (){
     List<Servant[,]> list = FindAllAlly ();

     foreach (Servant [,] testServants in list) {
         testServants.TakeDamage ( 5) // TakeDamage is on Servant script, but I cant use it, it isnt in the suggestions after typing "." so I guess it isnt getting the Servant script ? 
     }
 }

Looked on the forums about Lists and how to use them but couldnt find any answers or didnt know that I found it. What am I doing wrong ?

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

1 Reply

  • Sort: 
avatar image
0

Answer by hexagonius · Mar 28, 2017 at 05:23 PM

simply speaking, a list is an extensible array. in your case, it contains jaggedarrays of Servants. from what kind can see, you add servants to it and from the looks of your foreach, you just want servants, not arrays of them. make the list List<Servant> and it should work

Comment
Add comment · Show 1 · 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
avatar image cursedj07 · Mar 29, 2017 at 11:54 AM 0
Share

It worked ! Thank you , I tried with Servant alone at the beginning, but must have had a stupid error somewhere so assumed I needed arrays of them.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

c# foreach does not work 1 Answer

While going through a list of "debuffs", what would be the best way to add a new one if none are here or extend duration if it already exists. 0 Answers

c# foreach does not work 0 Answers

Playing Through a Queue/List of Audio Sources, One at a Time 1 Answer

How to remove an item from a list of custom variables 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