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
1
Question by amelnik · Dec 05, 2013 at 01:27 PM · c#queue

moving an object trough a queue of points

In my dart game I need a target to move between unknown amount of points that will be fed to the script via the inspector, then return to the starting point and start again. After searching the web, I chose to do it by a public array, that will be fed into a queue. After dequeuing each point it will be enqueued again into the queue. In code, it looks like this:

 public class LinearMovment : MonoBehaviour 
 {
     public Transform[] pointsArr;
     public float speed;
     
     private Vector3 targetPoint;
     private int index = 1;
     private Queue points = new Queue();
     
     void Awake()
     {
         foreach(Transform t in pointsArr)
         {
             points.Enqueue(t.position);
         }
         transform.position = Requeue<Vector3>(points);
         targetPoint = Requeue<Vector3>(points);
     }
 
     void Update()
     {
         if(transform.position == targetPoint)
         {
             index++;
             targetPoint = Requeue<Vector3>(points);
             print (targetPoint);
         }
         float distance = Vector3.Distance(transform.position, targetPoint);
         transform.position = Vector3.Lerp(transform.position, targetPoint, Time.deltaTime * speed / distance);
     }
 
     object Requeue(Queue q)
     {
         object o = q.Dequeue();
         q.Enqueue(o);
         return o;
     }
 
     T Requeue<T>(Queue q)
     {
         return (T)Requeue(q);
     }
 }

This code raised the following exception: "InvalidOperationException: operation is not valid due to the current state of the object". This exception appears to be in the Queue class line 176.

What causes this problem? How to solve it? Is it even a good way to do what I'm trying to do?

Thanks in advance.

Comment
Add comment · Show 3
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 etical · Dec 05, 2013 at 07:22 PM 0
Share

Have you checked in the inspector to make sure pointsArr isn't empty?

avatar image robertbu · Dec 06, 2013 at 06:39 AM 2
Share

All the work to use a queue seems a bit strange. From your description, all you are doing is walking a set of waypoints. There are many posts on waypoints including some code, plus there are third party solutions like iTween (free from the Asset store) that will do a smoothed walk.

avatar image amelnik · Dec 06, 2013 at 10:08 AM 0
Share

It was, in fact empty. When i switched from using array to queue i forgot that. Fixed. Thanks.

0 Replies

· Add your reply
  • Sort: 

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

promises in unityscript for Queueing 1 Answer

Initialising List array for use in a custom Editor 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