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 Benifir · Sep 10, 2013 at 12:42 PM · c#errorforeachassign-variable

Error assigning to a variable

How could I get around this problem?

Assets/Chat/Chat.cs(140,25): error CS1656: Cannot assign to `entry' because it is a 'foreach iteration variable'

 foreach(string entry in scrollingNotices) {
             GUILayout.BeginHorizontal();
             entry = GUILayout.TextField(entry);
Comment
Add comment · Show 7
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 cod · Sep 10, 2013 at 12:43 PM 0
Share

can u provide us more infos?

avatar image ArkaneX · Sep 10, 2013 at 12:48 PM 0
Share

Basically, it's not possible to assign value to foreach iteration variables. Please close two duplicates of this question, and then I'll try to provide more help.

avatar image AndyMartin458 · Sep 10, 2013 at 04:56 PM 0
Share

I don't really understand the previous answer. The main problem is not necessarily that it is readonly, I don't know about that. The real issue that the compiler is saving you from is that when you use a foreach loop, the variables of that loop are passed by copy. You'll need to access the variable in the scrollingNotices that contains that particular entry and modify it directly.

avatar image ArkaneX AndyMartin458 · Sep 10, 2013 at 08:15 PM 1
Share

Actually it is readonly. Internally, foreach uses IEnumerator, calling its $$anonymous$$oveNext method and accessing current element with Current property, which is readonly.

Elements are not passed by copy as well, because if they were, then modification of properties/fields of non-primitive types would be visible inside foreach scope only.

And besides, if they were passed as copies, then everybody would use foreach as a perfect deep cloner :)

avatar image AndyMartin458 AndyMartin458 · Sep 10, 2013 at 08:18 PM 0
Share

I believe the reference to the object is a copy. I could be wrong. But! I did not mean that the whole object was copied. That wouldn't make sense for a reference object. Having a copied reference to the object, you can still set variables of the object, and that still modifies the object whose reference was copied. But, changing the copied reference to point to a different reference, that does not modify the original object.

avatar image ArkaneX AndyMartin458 · Sep 10, 2013 at 09:03 PM 0
Share

Ok - I misunderstood you then, because I thought you were referring to copying the whole object.

Show more comments

1 Reply

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

Answer by aldonaletto · Sep 10, 2013 at 01:01 PM

The variable entry is readonly: it's assigned internally only by the foreach code, and you can't modify it. If you really want to allow edition of scrollingNotices elements via GUI, the code must be something like this:

 for (int i=0; i<scrollingNotices.Length; i++){
   GUILayout.BeginHorizontal();
   scrollingNotices[i] = GUILayout.TextField(scrollingNotices[i]);
   ...

But if you just want to display the elements of scrollingNotices, keep your original code and use GUILayout.Label instead:

 foreach (string entry in scrollingNotices) {
     GUILayout.BeginHorizontal();
     GUILayout.Label(entry);
     ...
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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Error assigning to a variable 0 Answers

Error assigning to a variable 0 Answers

Targeting Issue Error 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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