• 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 DevMerlin · Dec 17, 2015 at 09:19 PM · c#arrays

Queue results in Operation is not valid due to the current state of the object using Peek?

I am getting this error using Queue.peek in a dynamic world script:

InvalidOperationException: Operation is not valid due to the current state of the object System.Collections.Generic.Queue`1[UnityEngine.Transform].Peek () GameController.Update () (at Assets/Game Assets/Scripts/Gameplay/GameController.cs:62)

This error is being generated by a track generator script that uses Queue. Up to a point, I can build the track, push it into the queue, and then see it work, but as soon as Update tries to use the queue I created, it fails. The code itself is fairly basic and "seems" to work:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using WeightedRandomization;
 
 public class GameController : MonoBehaviour {
 
     public static GameController Instance;
 
     public GameObject Player;
 
     public Transform Track;
     public Transform StartPoint;
 
     public float Points;
 
     public float Distance;
 
     public int TrackRenderDistance = 14;
 
     public float RecycleOffset;
     public Vector3 NextPosition;
     public Transform NextPoint;
 
     public Queue<Transform> ObjectQueue;
 
     public bool Ready = false;
 
     void Awake()
     {
         Instance = this;
         ObjectQueue = new Queue<Transform>(TrackRenderDistance);
     }
 
     void Start ()
     {
         BuildTrack();
     }
 
     public void BuildTrack()
     {
         for (int i = 0; i < TrackRenderDistance; i++)
         {
             ObjectQueue.Enqueue((Transform)Instantiate(Track));
         }
 
         NextPosition = StartPoint.position;
 
         for (int i = 0; i < TrackRenderDistance; i++)
         {
             UpdateTrack();
         }
 
         Ready = true;
     }
     
 
     void Update ()
     {
         if (Ready)
         { 
             Transform Peek = ObjectQueue.Peek();
 
             if (Peek.position.z + RecycleOffset < Distance)
             {
                 UpdateTrack();
             }
         }
     }
 
     public void UpdateTrack()
     {
         Transform o = ObjectQueue.Dequeue();
         Track TrackPoint = o.GetComponent<Track>();
 
         o.position = NextPosition;
 
         NextPosition = transform.TransformPoint(TrackPoint.TrackConnectionPoint.position);
 
     }
 }

It is based off the older "Runner" tutorial for Unity3D, although has some changes to accommodate a different structure. Does anyone have any suggestions to fix this?

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

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

Answer by phil_me_up · Dec 17, 2015 at 09:34 PM

I would guess that your problem is because you're calling Dequeue in UpdateTrack which will be reducing the number of items in the list by 1. At some point your queue will be empty so calling Peek() in the Update routine will return null.

My suggestion is that you actually check the size of the queue is greater than 0 before calling Peek and if it is 0, don't continue. I'd also suggest checking that what you Peak is not null, and the same with what you Dequeue, just as a precaution. Example:

 void Update ()
      {
          if (Ready)
          { 
            if(ObjectQueue.Count > 0)
            {
              Transform Peek = ObjectQueue.Peek();
  
              if (Peek.position.z + RecycleOffset < Distance)
              {
                  UpdateTrack();
              }
            }
          }
      }
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 DevMerlin · Dec 17, 2015 at 10:01 PM 0
Share

HA! I feel pretty stupid, yeah. That was the cause, but because it wasn't being added back into the queue after changes were made. I completely missed that in my original code somehow.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

38 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity c# indexoutofrangeexception: array index is out of range 1 Answer

Declaring an Array as a combination of multiple other arrays? 1 Answer

if statement only ever triggering the first condition even if it's false and the second one is true 0 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