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 NotEnoughLemons3D · Oct 24, 2016 at 01:00 AM · c#arrayscript error

C# Index is Out Of Range (But it is not)

     int p = System.Array.IndexOf (AlivePlatforms,Plat.gameObject);
         for (int n = p; n < AlivePlatforms.Length; n++) {
             if (AlivePlatforms [n + 1]) {
                 if (n + 1 < AlivePlatforms.Length -1) {
                     AlivePlatforms [n] = AlivePlatforms [n + 1];
                 }
             }
             else{
                     AlivePlatforms[n] = null;
                     break;
                 }
             print ("N: " + n);
         }


I have an array called ActivePlatforms. It has a length of 20, so 0-19 should be acceptable indices. I define a variable called p to be the index of a certain game object. In my test runs p has always below 2 or 3. The for loop is supposed to run through a loop starting at p and basically remove p while moving each other value above it down by 1. There is only about 8 objects in the 20 size AlivePlatforms array.

So basically what I am trying to do is remove an element form the middle of an array and move all the elements above (at a greater index) it down one. Yet it says index is out of range. The print statement was put in to tell me at what index it is out of range. It never prints past 5 before it tells me, but the Arrays length is 20. I even put in an if statement to make sure n is always less then Aliveplatforms length. I can't figure out how the index is out of range when it has no possible way to be greater or equal to the length, and in my test runs never became greater than 5.

Thank you for your help.

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 Eric5h5 · Oct 24, 2016 at 02:35 AM 0
Share

Your print statement is at the bottom of the loop, so it won't run if there was an error. Also your code makes assumptions about what p might be, which is never a good idea. If the condition is "n < AlivePlatforms.Length" and you immediately try to access "AlivePlatforms [n + 1]", that's bound to fail.

As an aside, any reason you're not using a List and Remove/RemoveAt rather than trying to do this manually?

2 Replies

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

Answer by Bunny83 · Oct 24, 2016 at 02:18 AM

Well, this line:

 if (AlivePlatforms [n + 1]) {

Will fail for "n == 19".

I guess your condition should simply be

 if (n+1 < AlivePlatforms.Length)
 {
     AlivePlatforms [n] = AlivePlatforms [n + 1];
 }
 else
 {
     AlivePlatforms[n] = null;
     break;
 }

You could also use

 if (n+1 < AlivePlatforms.Length && AlivePlatforms[n + 1] != null)

To only iterate up to the first null value.

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 NotEnoughLemons3D · Oct 24, 2016 at 01:48 PM 0
Share

Thank you for your answer. The problem ended up being that n would sometimes equal -1. I guess this is the default value returned if Array.IndexOf() does not include the object you are searching for. Though I am pretty sure my object is in the array, I just added a check to make sure n did not equal -1 and it seems to work fine. Thanks again!

avatar image
0

Answer by NotEnoughLemons3D · Oct 24, 2016 at 02:16 AM

Figured out the issue. Array.IndexOf() was returning -1.

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 · Oct 24, 2016 at 02:20 AM 0
Share

Of course that's an additional problem if the object isn't inside the array.

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

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

Move enemy to a specific movepoint with an array (C#) 1 Answer

How to reference UI Panels, in an array for x and y? 0 Answers

Null reference exception in an if statement that checks for it. 1 Answer

How to make a 2D array of buttons? 2 Answers

C# Random String from List 2 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