Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by YukariYakumo · Sep 22, 2013 at 10:20 AM · listforeach

Change global state in list

 public enum MyState
 {
 A,B,C
 }
     
 public class ABC
 {
 int length;
 }
 
 public List<ABC> abcs = new List<ABC>();
         
 public MyState state = MyState.A;
 
 void Start()
 {
 for(int i = 0 i < 5;i++)
 {
 ABC temp = new ABC();
 tmp.length = 1 + i;
 abcs.add(temp);
 }
 }
 
 void Update(){  
 foreach(ABC a in abcs)
 {
 if(a.length == 1)
 state = MyState.B;
 }
 }

I want to change my custom enum when one is satisfy my condition in collection. But only last one in collection will do , how to solve it ?

Thanks in advance.

Comment
Add comment · Show 2
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 AdenFlorian · Sep 22, 2013 at 10:45 AM 0
Share

What is ABC and abcs?

I'm having trouble understanding your question.

avatar image YukariYakumo · Sep 22, 2013 at 11:18 AM 0
Share

Expanded..

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ArkaneX · Sep 22, 2013 at 11:48 AM

You have to do

 foreach(ABC a in abcs)
 {
     if(a.length == 1)
     {
         state = MyState.B;
         break; // this causes foreach loop to stop and not check other ABC elements 'length' field
     }
 }

You could also use LINQ for this (requires using System.Linq;) and just write

 if(abcs.Any(a => a.length == 1))
 {
     state = MyState.B;
 }
Comment
Add comment · Show 8 · 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 YukariYakumo · Sep 22, 2013 at 12:24 PM 0
Share

I tried first method , It became only first one in list can change state..

avatar image ArkaneX · Sep 22, 2013 at 12:38 PM 0
Share

$$anonymous$$aybe you didn't add curly brackets under if? Please note, that in your code only one command is executed after if. In my code, there are two commands, so they have to be wrapped in curly brackets.

avatar image YukariYakumo · Sep 22, 2013 at 12:49 PM 0
Share

I'm sure I've added curly brackets.

avatar image ArkaneX · Sep 22, 2013 at 01:10 PM 0
Share

How did you test it then? If you have a list with two elements, where length of first is 2 and length of second is 1, then does your foreach loop breaks before reaching the second one???

avatar image Jamora · Sep 22, 2013 at 03:55 PM 0
Share

I think he needs to iterate the abcs in reverse. Possibly only if (abcs[abcs.Count-1].length == 1).

Show more comments

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

17 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

Related Questions

A node in a childnode? 1 Answer

How to find all similar elements in a list? 1 Answer

foreach a array inside a generic list 1 Answer

How do I find a local variables index in a foreach loop? 1 Answer

how do i check if a list contains elements from another list? 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