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 /
avatar image
0
Question by Luponius · Mar 28, 2016 at 10:05 PM · c#listprocedural generationalgorithmqueue

InvalidOperationException: Operation is not valid due to the current state of the object

While (queue.Peek() != null) throws this error. changing queue.Peek() to just queue skips that section of error and moves on to the next line of var q = queue.Dequeue();

I do know that the queue itself isn't empty, however curiously enough there are 3 null entries at the end, I'm not sure if this is normal for queues as they're grow-able or whatnot, but apparently any of those two Peek() and Dequeue() operations causes this. Enqueue() does not however as you can see it being done multiple times above the problem area.

The original code I'm basing myself off of instead uses queue.Any(), but this is apparently not available based on Unity's intellisense since it doesn't give it up as an option, even though it did somehow work out in the other project using Any(), maybe it was using a different compiler.

Appreciate any help.

 private void AssignTileElevations ()
 {
     var queue = new Queue<Tile>();

     foreach (var q in islandMap) {
         q.water = !inside(q.point);
     }

     foreach (var q in islandMap)
     {
         // The edges of the map are elevation 0
         if (q.border)
         {
             q.elevation = 0;
             queue.Enqueue(q);
         }
         else
         {
             q.elevation = float.PositiveInfinity;
         }
     }

     while (queue.Peek () != null) {
         var q = queue.Dequeue();

         foreach (var s in q.adjacent)
         {
             var newElevation = 0.01f + q.elevation;
             if (!q.water && !s.water)
             {
                 newElevation += 1;
                 if (needsMoreRandomness)
                 {
                     newElevation += UnityEngine.Random.value; ;
                 }
             }
             if (newElevation < s.elevation)
             {
                 s.elevation = newElevation;
                 queue.Enqueue(s);
             }
         }
     }
     Debug.Log ("Elevations Set");
 }
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 Luponius · Mar 29, 2016 at 10:02 AM 0
Share

No suggestions? :(

1 Reply

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

Answer by Luponius · Mar 29, 2016 at 05:51 PM

Apparently the problem was Peek(). For some reason you can't peek an array if it doesn't exist and even if it does exist it will still throw invalidoperation exceptions... I have no idea why this is but swapping to queue.Count > 0 instead of peek solved the issue.

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

128 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 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 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 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 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 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 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

Insert string into empty list at a specific index 0 Answers

Getting certain values from an item in a list 0 Answers

Difference between the various "OverlapCollider" functions? 0 Answers

How to randomly create a tile based maze 0 Answers

Mesh wont render although there are no errors! 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