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 SomeRandomGuy · Mar 11, 2013 at 01:14 PM · listcounterindexoutofrangeexceptioncycle

Cycling through a List with constantly changing element count. Running into problems D:

Hi,

I've basically got this little script that is supposed to cycle through enemies whenever I press the shift key. It does this, but since the List used to store the enemies keeps changing in size rather fast.

A simple counter runs into index out of range errors, seemingly because when the list would be filled like this: element 0=A, 1=B, 2=C And have enemy C selected, enemy A can suddenly drop out of the list.

Making the list count 2, while I'm trying to select the third, now non existing element. I believe this is the reason why I keep getting these errors, but I'll post a snippet anyway.

 function switchLockOnTarget()
 {
     if(Input.GetKeyDown(KeyCode.LeftShift))
     {
         
         if(curLockOn+1>=lockOnTargets.Count)
         {
             curLockOn = 0;
         }
         else if(curLockOn+1<=lockOnTargets.Count)
         {
             curLockOn = curLockOn+1;
         }
     }
 }

Am I missing something simple here, or do I need to consider using different methods of cycling through the list?

Thanks in advance,

~Nick

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Statement · Mar 11, 2013 at 02:24 PM

If you have an index to an item in a list, it would make sense to signal when you modify that list.

In the scenario where an item was removed from the list, any indices that reference a greater index should be decremented by one to maintain the correct index.

Perhaps something like this? There may be typos, I just wrote some code without checking that it compiles.

 function RemoveItem(index : int) {
    lockOnTargets.RemoveAt(index);
    if (curLockOn == index) {
       // The target was lost, what should we do now?
    }
    else if (index < curLockOn) {
       // Adjust for items removed "below" us.
       curLockOn--;
    } 
 } 
Comment
Add comment · Show 2 · 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 SomeRandomGuy · Mar 11, 2013 at 02:41 PM 0
Share

Hmm...so, if I'm reading this right, I'd use the RemoveItem function with the index being the number of whatever enemy was just lost to remove the enemy from the list, right? Then check if the target was the one that moved away by comparing it to index. if it isn't, and the index is lower than current target, then we take one from the current target. I think I get it, now that I've written it all out...haha! I'm gonna try this and see if that did the trick.

Edit: Unless I'm getting the wrong idea of what your function does, I think it won't work. still getting the errors. I'm a bit confused...x]

avatar image Statement · Mar 11, 2013 at 02:44 PM 0
Share

Or, ins$$anonymous$$d of relying on the actual index, keep a reference to the object in the list ins$$anonymous$$d. That way it doesn't matter if the indices change. Then you can use list.IndexOf(myTarget) if you need to find the index for any reason, for example, when you want to move to the next one. If you list becomes very large, then IndexOf could incur some performance overhead since it has to go through each item in the list.

avatar image
0

Answer by Bunny83 · Mar 11, 2013 at 02:34 PM

I would suggest to use a LinkedList instead and hold a reference to the node object that is currently selected. That way the order / count can change without breaking the reference to the "next" item. Of course if the payload object (your gameobject) gets destroyed you should select the next / prev element.

I've never tried this myself, but i think it should work.

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 Bunny83 · Mar 11, 2013 at 02:47 PM 0
Share

Unfortunately the LinkedListNode loses it's Next and Previous references when it's been removed from the list, so if the current object is being destroyed / removed you have to find a new one, but i think that's ok since it's no longer part of your cycle.

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

13 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

Related Questions

A node in a childnode? 1 Answer

Why Is TextMesh Off by One ??? 0 Answers

cycle trhu an Array of gameobjects? 1 Answer

Arguement out of range exception on a lists index 3 Answers

List of type Button has size set to zero at runtime 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