• 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 getyour411 · Aug 22, 2013 at 08:50 PM · c#array

C# Array remove/add help IndexOutofRange

There are about a billion examples from UA/Google and 99% say 'Use Lists instead' which is fine but before I convert to List I'd like to understand what I'm doing wrong here for other things where an array makes more sense. I've included comments and output from debug.log below, I get an ArrayIndexOutofRange error:

 public void RespawnMob(GameObject mobToRemove) {
 
     Transform parentSpawner = mobToRemove.transform.parent;
     
     Debug.Log ("Current length of mobs: " + mobs.Length);
     // 4
     
     GameObject[] mobtemp = new GameObject[mobs.Length-1];
     int i=0;
     
     foreach (GameObject go in mobs) {
         if (go != mobToRemove) mobtemp[i++] = go;
     }
     
     Debug.Log ("removed dead guy, length of holding array is " + mobtemp.Length);
     // 3
     
     mobs = mobtemp;
     
     
     // Tried doing both in one step but that didn't work so tried doing it twice
     
     
     GameObject[] mobtemp2 = new GameObject[mobs.Length+1];
     // 4
     
     Debug.Log ("New mobtemp2 size is " + mobtemp2.Length);
     // 4
     
     int y=0;
     foreach (GameObject go in mobs) {
         mobtemp2[y++] = go;
     }
     
     Debug.Log ("Spawning mob @ " + parentSpawner.name);
     
     int daX = mobtemp2.Length-1;
     Debug.Log ("Attempting create @ " + daX);
     // 4
     
     // ArrayIndexoutofRange here
     mobtemp2[daX] = Instantiate(mobPrefabs[0],parentSpawner.position, Quaternion.identity) as GameObject;
 
     Debug.Log ("Attempting reassignment from holding array now with length of " + mobtemp2.Length);
     mobtemp2[daX].transform.parent = parentSpawner;
     
     mobs = mobtemp2;
 
     Debug.Log (mobs.Length);
 

 }

}

Edit: Upated code with suggested fixes and it's working. Now to convert to a List, I'll open new question if I get stuck.

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 ArkaneX · Aug 22, 2013 at 09:41 PM 0
Share

I'm glad it is working now. But before you convert to list, one little suggestion. If you want to simply replace one dead mob with another, then you can do:

for(int i = 0; i < mobs.Lenght; i++) { if(mobs[i] == mobToRemove) { // Destroy(mobs[i]); mobs[i] = Instantiate(.....); } }

Unless you want to have your new mob at the end of this array, the above code should work.

2 Replies

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

Answer by ArkaneX · Aug 22, 2013 at 08:55 PM

Arrays are 0-based. That means the first element has index 0, second element has index 2, etc. Last index is equal to length of the array minus 1.

In line 32 you select length of your array, and in line 37 you pass this value as an index. You should do:

 int daX = mobtemp2.Length - 1;

if you want to obtain index of last element.

Comment
Add comment · Show 4 · 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 getyour411 · Aug 22, 2013 at 09:07 PM 0
Share

Thanks both for your input; this fixed it.

avatar image getyour411 · Aug 22, 2013 at 09:18 PM 0
Share

Ack! The first mob I killed it worked (so I thought it was fixed) but the result was my mob[] array only had him all my other mobs were wiped out of the array, so the NEXT mob I killed sent this thing into a tizzy. I think I see why, in the second pass I never copied the stuff via the foreach - I'll try that now.

avatar image ArkaneX · Aug 22, 2013 at 09:32 PM 0
Share

So, do you still have some issue with this code?

avatar image getyour411 · Aug 22, 2013 at 09:32 PM 0
Share

Nope, all set now thank you

avatar image
0

Answer by Slobdell · Aug 22, 2013 at 08:58 PM

It's this line here

     mobtemp2[daX] = Instantiate(mobPrefabs[Random.Range(0,mobPrefabs.Length)],
 

Should be

     mobtemp2[daX] = Instantiate(mobPrefabs[Random.Range(0,mobPrefabs.Length -1)],


Say your array has length of 10. You will have indexes of 0-9. So when you're grabbing a random with a range up to ten, eventually you will try to get index 10 of the array, when in fact the 10th slot in the array will have an index of 9. Make sense?

Comment
Add comment · Show 6 · 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 getyour411 · Aug 22, 2013 at 09:03 PM 0
Share

I thought RandomRange on ints will only pick X to Y-1 where the -1 is implicit, so if Y is 10 it would only possibly return 0-9 (unless X and Y are equal in which case it returns X). But the other stuff makes sense, making changes now

avatar image Slobdell · Aug 22, 2013 at 09:05 PM 0
Share

No, those numbers are inclusive on both

avatar image getyour411 · Aug 22, 2013 at 09:07 PM 0
Share

I double checked that here for Ints says max is exclusive?

http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html

avatar image Slobdell · Aug 22, 2013 at 09:13 PM 0
Share

Wow, you're right it does say that. That is completely bizarre that it says there is different behavior for int and float. $$anonymous$$akes no sense, I still don't believe it. I'm going to experiment when I get home

avatar image getyour411 · Aug 22, 2013 at 09:19 PM 0
Share

I did try changing that bit to just [0] to bypass any funky stuff but that didn't resolve the issue.

Show more comments

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

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

How to properly create a 2 dimensional array of an object. [C#] 1 Answer

Find Closest Object by Name 0 Answers

comparing variables of gameobjects in an array 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