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 Hamesh81 · Jul 29, 2013 at 08:08 AM · arrayloopbooleanboolforeach

How can I do a foreach loop for an array of booleans?

I have an array of booleans in C#, and after a specific input I would like to loop through and set all of these booleans to false. But I am getting the following error with the loop code:

Cannot assign to buttonBoolean' because it is a foreach iteration variable'

And here is the code:

 public bool[] inputBool;
 
 foreach (bool buttonBoolean in inputBool) {
                buttonBoolean = false;
         }

The booleans in the array all come from the one script which is attached to one gameobject in the scene. I've used a loop similar to the above to for example turn off/deactivate all the gameobjects in an array, so I'm not sure why it isn't working for turning off all the booleans in an array.

Comment
Add comment · Show 1
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 nixcs2512 · Jul 29, 2013 at 08:19 AM 0
Share

If i'm not wrong, you can't change any properties of variable while using foreach. Use simple for loop to do that.

1 Reply

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

Answer by Lovrenc · Jul 29, 2013 at 08:19 AM

The problem is not that your array is boolean, it is that you are trying to assign value to buttonBoolean in foreach loop. It is read-only as every foreach iteration variable is. Check this

You could do this instead:

     public bool[] inputBool;
      
     for(int i = 0; i < inputBool.Length; i++) {
       inputBool[i] = false;
     }


Comment
Add comment · Show 4 · 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 Hamesh81 · Jul 29, 2013 at 08:52 AM 0
Share

Yes that's it! Thanks a lot you're a champ. Btw the link you posted above doesn't have a proper url ;)

avatar image Lovrenc · Jul 29, 2013 at 08:57 AM 1
Share

Fixed link.

avatar image CHPedersen · Jul 29, 2013 at 10:00 AM 0
Share

As a side note, just for the hell of it: The default value of bool is false. So unless this has to take place on a regular basis because you need the array reset to all false, then there's no reason to do it.

avatar image Hamesh81 · Jul 29, 2013 at 12:55 PM 0
Share

Yes of coarse, the above is only a snippet of a much larger piece of code.

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

18 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

Related Questions

Not all code paths return a value in boolean loop 2 Answers

how do i make a loop for a boolean? 2 Answers

Get the number of false bools in GameObject[] (array) 3 Answers

How can I move back to Element 0 in an array to reset a loop? 1 Answer

Foreach with GameObject.Find() 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